As we have seen in the case of the Kraalingen simulation approach, interfaces are a powerful modeling artifact to design flexible software that can be reused and extended, and is resilient to changes. Interfaces replace the hardwired associations between classes with flexible associations between a class and an interface, opening the way for use to many other potential classes or components that provide similar behavior and implement the same interface. Interfaces make the communication between two classes flexible. When two classes are linked through an association, the link is rigid, as it is a one-to-one link. None of the classes can be used separately. Using interfaces instead of classes transforms the one-to-one link into to a one-to-many link. Therefore, many other classes or components can be used in the system, allowing for substitutability among classes/components that implement the same interface.
Some questions may be asked right away. When should we use interfaces? Should we use interfaces any time that two classes are associated? The issue of when and how to use interfaces is related to the designer's experience and the future needs for extending the system to incorporate new behavior. An interface can be considered as an electrical plug in a house. When the blueprints for the electrical wires are drawn, the designer has in consideration the places where potential appliances can be used. After the electrical installations are finished, one can only add new appliances in the places where there is a plug to be used. Interfaces are conceptually the same. We will use an interface at a point in the system that we see future development, change, or potential for substitutability. We would strongly recommend [CMK99]; the book Java Design Building Better Apps & Applets is excellent source of information and ideas about using interfaces as a modeling tool.
Is there any drawback of using interfaces? The answer is, not really. Interfaces make the system more flexible at the price of slightly increasing the complexity of the system. A diagram that uses interfaces may be more difficult to be understood as it contains points of connection to many potential classes instead of one single class. There is a widely accepted agreement that the increase in complexity of a class diagram as a result of using interfaces is rightly justified by the benefits offered by interfaces in the design process.
Was this article helpful?