Interfaces

From TBwiki
Jump to: navigation, search

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.


Usage

A practical example is the implementation of the the ToolPack wrapper classes on top of the CMC API:

CMC interfaces.jpg

As shown in the figure above, the CMC library does not actually knows which classes implements the different functional APIs. It only knows of the pointers to the interfaces to each different service. Whether or not these interfaces are implemented into a single object or multiple objects is irrelevant to the CMC library.

Caveats

Be aware that some interface member functions may have default implementation while some other absolutely need SENTENCE IS INCOMPLETE



Return to the Toolpack User Guide

Personal tools