The Adapter Pattern
Here's a quotation from the GoF book:
"[An adapter] converts the interface of a class into another interface the client expects. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces."
The Adapter design pattern follows two important design principles (both quoted directly from the GoF book):
- "Favor object composition over class inheritance."
- "Program to interfaces, not implementations."
Motivation
Consider the following situation. You have a nice, useful piece of code, but you can't easily use the code in an existing application. The "useful" code has an interface, but the existing application expects a different interface. Using the Adapter design pattern, you can use the useful code in the existing application.
Implementation
Figure 1 shows the official UML diagram for the Adapter pattern.

Figure 1. The official UML diagram for the Adapter design pattern.
Here's a walk-through of the parts of the UML diagram.
- A
Client class expects a certain interface (called the Target interface).
- An available interface doesn't match the
Target interface.
- An
Adapter class bridges the gap between the Target interface and the available interface.
- The available interface is called the
Adaptee.
- The
Adapter class stores a copy of adaptee, an instance of the Adaptee class.
What this UML diagram needs is a concrete example. Consider some electronic devicesGame Boys, cell phones, and other devices. You plug the devices into your car's cigarette lighter socket or the wall outlet in your house. Both car charging and home charging require adapter cables. The UML diagram for this concrete example is in Figure 2.

Figure 2. The UML diagram for the device charging application.
The elements in Figure 2 have parallels in Figure 1.
- A Game Boy or cell phone device (the
Client in Figure 1) expects a certain interface. This interface may be a mini-plug, a USB port, or some other gizmo. The general name for such an interface is the Target interface.
- An available
Charger interface (a power source, such as a wall outlet or cigarette lighter socket) doesn't match the Target interface.
- A
DeviceAdapter (called an Adapter in Figure 1) bridges the gap between the Target interface and the Charger interface.
- The
Charger interface is called the Adaptee.
Figure 3 shows yet another view of all this terminology.

Figure 3. Figure 3: Visualizing the device charging application.
The source code for the UML diagram in Figure 2 is in Listings 1 to 7. A main method to test the code is in Listing 8.
The Device interface declares a method called getCharge() which is the interface that clients (cell phones and Game Boys) use. In the meantime, the Charger interface declares a different interface methoda method named chargeDevice().
The main application (Listing 8) declares Charger instances (CigaretteLighter and WallOutlet) and Device instances (GameBoy and CellPhone). An adapter for each device allows a charger to charge the device. That's where the fun begins. The main method creates an instance of DeviceAdapter for each of the devices:
// create the device adapters
Charger device01 = new DeviceAdapter(gameBoy);
Charger device02 = new DeviceAdapter(cellPhone);
Notice two things about the code in DeviceAdapter (Listing 7):
- The code implements the
Charger interface, so this code must implement the Charger interface's chargeDevice() method.
- The code's device field stores an instance of a
Device type.
The DeviceAdapter uses its stored device object to implement the chargeDevice() method.
public void chargeDevice(String charger)
{
device.getCharge(charger);
}
Each DeviceAdapter instance has a Device instance. Another name for this "has a" relationship is "composition." And the use of composition underlies many design patterns.
The following code displays the output of the ChargeDevices program (Listing 8).
-- Device Charging Application --
The Game Boy is being charged by the cigarette lighter...
The Cell Phone is being charged by the cigarette lighter...
The Game Boy is being charged by the wall outlet...
Listings 1 8
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|