advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorial: Resistance is Futile: How to Make Your Java Objects Conform with the Adapter Pattern:

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 devices—Game 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 method—a 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

How to Add Java Applets to Your Site

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.

 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 38
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity
Microsoft's Novell Investment Tops $340M
Fedora 10 Takes Shape
IBM Gives a Mobile Voice to Developers
Inadequate Tools Send Software Down the Drain
USB 3.0 One Step Closer to Reality
Would-Be Linux Contributors May Get a Leg Up

Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element
Setting Up and Running Subversion and Tortoise SVN with Visual Studio and .NET
Java/JRuby Developers, Say Open 'Sesame' to the Semantic Web
Interpreting Images with MRDS Services
DevXtra Editors' Blog: Executives Avoiding Cloud Computing in Droves
Q&A with James Reinders on the Intel Parallel Studio Beta Program
The Pros and Cons of Outsourcing Enterprise Emails
Hosting Options: Shared or Dedicated Server

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers