Initial Thoughts:
Interfaces are mechanisms to provide access to something, e.g., an electrical outlet in a room
provides an interface to the electrical distribution system in the building containing the room.
In the context of software systems there are two distinct meanings of the word interface:
-
A class interface is the collection of public methods and properties (C# and Java) of the class.
They provide the means for another method of the same or another class to use the facilities of
the class.
-
The second meaning of the word interface is a grammatical construct that provides a contract
for communication with all instances of classes that implement the interface.
In C#, Java, and C++\CLI an interface is declared with the interface keyword,
has no constructors, and all its methods are overrideable. Interfaces may also declare properties
and delegates.
Native C++ does not have an Interface construct. However, it is easy to construct the equivalent
with a struct that has all pure virtual methods and implements an empty virtual destructor.
For the remainder of this discussion we will use the second definition, e.g., a contract for communication.
So why use interfaces?
Interfaces do several important things to support well crafted code:
-
When client code binds to an instance of a class, using an interface the class implements, that client
doesn't bind to any of the class's implementation details, as long as the client didn't have to
create an instance of the class it's using. We support this by providing an object factory that returns
to the client a reference with the type of the interface, not the type of the class.
Summary: