Saturday 17 November 2012

Interface (Object Orientation)

An interface (English interface) is used in object-oriented programming agreeing common signatures of methods that can be implemented in different classes. The interface shall state to which methods are available and must be present.

Technical Details

An interface specifies which methods are available and must be present. In addition to these syntactic definition is known as a contract should always be defined over which the meaning is defined (in terms of preconditions and post conditions) of different methods - i.e., their semantics. The contract is usually defined only informally in the documentation or an external specification of the interface, but is also formal specification languages ​​such as OCL is available. Some programming languages ​​such as Eiffel can also directly syntactic ways of establishing a contract.
Interface (Object Orientation)

Interfaces represent a guarantee with respect to the existing methods in a class. They indicate that all objects that have this interface can be treated equally.

In some programming languages ​​that do not support multiple inheritance (such as Java), interfaces can be used to define compatibility between classes that do not inherit from each other: the interface relationships are not bound by the strict class tree. This interface declarations are often explicitly marked as such (as with the interface keyword). As a replacement for multiple inheritance, interfaces are not, however, as they only define methods and their parameters and do not allow inheritance of functionality.

Declaration

Other languages ​​(usually those that support multiple inheritance like C + +) but know the concept of interfaces, but treat them like ordinary classes. One then speaks of abstract classes. Sometimes their own language (called Interface Definition Language, IDL) used for the declaration of the interface - is mostly in the middleware systems such as CORBA or DCOM the case. Object-based languages ​​without strong typing usually know no interfaces.

Definition of constants

In some programming languages ​​like Java or PHP, it is possible to declare constants in an interface definition. All implementing classes are then these constants.

Classification of interfaces

Interfaces can be classified along two independent criteria: generality and usefulness. Regarding the general distinction between general and context-specific interfaces on the benefits between offering and enabling interfaces.
  • Common interfaces include the entire public interface of the callee. They are used to separate the used interface specification from its implementation.
  • Context-specific interfaces do not cover the entire public interface of a class, but only specific aspects of this class. They allow you to use objects of a class in particular roles. For example, a buffer can be used for reading or writing. For each of these access "issues" can be a separate interface exist.
  • Interfaces are offering, if the caller turns over the interface to the callee. This is the typical and most common case in the use of interfaces.
  • Enabling end interfaces are present if the callee reversed or even a third component is the real beneficiary of the interface. For example, an object (or its class) implements the interface Printable. Such an object can then be handed over to a printer for printing. Obviously provides the object, which meets the interface is not the service, but it only allows.

A special case is called marker interfaces that do not require any methods to be evaluated through Introspection mechanisms at runtime.

Naming Conventions

In some programming languages, it is customary to make interfaces by special prefixes or suffixes recognizable. So often an "I" prefix (for interface) or appends an "IF" and "Interface". This has no technical reasons but was chosen as a means to improve readability and thus maintainability. The above example would read IKonto interface Account, Account Interface or KontoIF.

Advantages
  • Interfaces are the names recognizable as such.
  • Implementing classes can have a simpler name.

Disadvantages

  • Interfaces should not be recognized as such in the name, because you as a user of other objects, only the interface (i.e. public methods) should keep in mind. [1]
  • Interfaces can be regarded as the essential element of programming. Therefore, it makes more sense to add to the names of the implementations with prefixes or suffixes.
  • Interfaces are particularly useful when there is more than one implementation, so that the implementing classes are already named with prefixes and suffixes.

No comments:

Post a Comment