The adapter pattern is used to convert the interface of one class into one another interface that the client expects. This allows classes to collaborate when they may not have been originally designed to do so. The Adapter pattern is also called a Wrapper.
Suppose you acquire a class that needs to use a service of some sort and provides an interface for it. Further, you have another class that provides the service, but does not conform to the required interface. The Adapter pattern can be used to make the two classes compatible.
In the typical case, we have a class that we may not have the software for, or it is a general-purpose class that has a well-defined interface that it uses to acquire services. In both cases, the client and the interface cannot be changed.
You may also encounter cases where you want to dynamically determine which methods of another object that will be called by a client object. And, you want to do this without the client knowing anything about the called object.
We create a class that converts, or "adapts" the client's interface to the service provider class's interface. It performs the conversions required to connect the client's calling interface to the interface called in the server. One may think of an electric adapter that takes 120V AC electric from a wall socket and converts it to 9V DC that an electronic device might need. The device calls for 9V DC, but the electric source supplies 120V AC.
The general pattern for the Adapter pattern is as follows:
There are two variations of the general pattern, however. The first is called the class adapter, where the Adapter is a subclass of the Adaptee, as shown here:
In some cases it is not possible, nor practical to have the Adapter subclass the Adaptee. In this case we use the object adapter variation, shown here:
![]()
A code example for the class and object adapters is in this zip file.
Bridge
Decorator
Proxy
Modified: 21-Jan-2004
Gary Pollice