Actions

Interfaces: Difference between revisions

(New page: Interfaces are basically a set of member functions that act as an API to an object. Implemented as pure-virtual base classes (i.e. can only be used through inheritance), these APIs are use...)
 
No edit summary
Line 1: Line 1:
Interfaces are basically a set of member functions that act as an API to an object. Implemented as pure-virtual base
Interfaces are basically a set of member functions that act as an application programming interface (API) to an object. Implemented as pure-virtual base classes (i.e., they can only be used through inheritance), these APIs are used by other modules of the system to interact with all types of objects without knowing their specific definitions (i.e., having to include an header file with the description of the class).
classes (i.e. can only be used through inheritance), these APIs are used by other modules of the system to interact with
 
all types of objects without knowing their specific definitions (i.e. having to include an header file with the description
One can see these interfaces as a set of functions that everyone knows how to call. As long as a class inherits from an interface, any object that knows of this interface can use the class. This design approach is commonly used to create systems where the implementation of a given functionality is irrelevant or needs to be replaceable.
of the class). One can see these interfaces as a set of functions that everybody knows how to call. As long as a class
 
inherits from an interface, any object that knows this interface can use the class. This design approach is commonly
For example, a database interface would define member functions to read and write records from a database. Multiple classes deriving from that interface could implement it differently such as using an XML file instead of a real database or storing the information into random access memory (RAM). The benefit from this approach is that any other piece of code in the system only needs to be passed a pointer to the interface regardless of the type of the class implementing it. Thus, there is no knowledge of the actual class implementing the interface.
use to create systems where the implementation of a specific functionality is irrelevant or need to be replaceable. For
example, a database interface would define member functions to read and write records from a database. Multiple
classes deriving from that interface could implement it differently such as using an XML file instead of a real database
or storing the information into RAM. The benefit from this is that any other piece of code in the system only need to
be passed a pointer to the interface regardless of the type of the class implementing it. Thus, there is no knowledge of
the actual class implementing the interface.

Revision as of 14:52, 7 May 2009

Interfaces are basically a set of member functions that act as an application programming interface (API) to an object. Implemented as pure-virtual base classes (i.e., they can only be used through inheritance), these APIs are used by other modules of the system to interact with all types of objects without knowing their specific definitions (i.e., having to include an header file with the description of the class).

One can see these interfaces as a set of functions that everyone knows how to call. As long as a class inherits from an interface, any object that knows of this interface can use the class. This design approach is commonly used to create systems where the implementation of a given functionality is irrelevant or needs to be replaceable.

For example, a database interface would define member functions to read and write records from a database. Multiple classes deriving from that interface could implement it differently such as using an XML file instead of a real database or storing the information into random access memory (RAM). The benefit from this approach is that any other piece of code in the system only needs to be passed a pointer to the interface regardless of the type of the class implementing it. Thus, there is no knowledge of the actual class implementing the interface.