Design Patterns

Adapter



Design Pattern Type: Structural

GoF Statement of Intent:


Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Brief Overview:



Allows classes to work together that normally could not do so. The adapter accepts calls from one class and translates them into appropriate calls to another class and then translates return values from the called class to that expected by the calling class.
UML Diagram:

Discussion and In-class Example:

The Adapter Design Pattern is one that is conceptually very easy to understand. We use adapters, such as the electrical plug adapter every day. Adapters are a "go-between" class which takes messages from one class and translates them into the type of message that another class expects.



With our tongue somewhat in out cheek we looked again at the Duck class and found a new foul had moved into the neighborhood, Turkey. In order for Turkey objects to fit into out Sim-U-Duck application we needed a TurkeyAdapter which is a subclass of Duck and can take calls to the Duck fly() and quack() function and translate them into the appropriate calls to gobble() and fly() in a Turkey object.