The Adapter Pattern
ht=1>
Yahoo! is not affiliated with the authors of this page or responsible for its content.
The Adapter Pattern
1
Design Patterns In Java
Bob Tarr
The
Adapter
Pattern
Bob Tarr
Design Patterns In Java
The Adapter Pattern
2
2
The Adapter Pattern
The Adapter Pattern
l
Intent
é
Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.
l
Also Known As
é
Wrapper
l
Motivation
é
Sometimes a toolkit or class library can not be used because its interface is
incompatible with the interface required by an application
é
We can not change the library interface, since we may not have its source
code
é
Even if we did have the source code, we probably should not change the
library for each domain-specific application
2
Bob Tarr
Design Patterns In Java
The Adapter Pattern
3
3
The Adapter Pattern
The Adapter Pattern
l
Motivation
é
Example:
Bob Tarr
Design Patterns In Java
The Adapter Pattern
4
4
The Adapter Pattern
The Adapter Pattern
l
Structure
é
A class adapter uses multiple inheritance to adapt one interface to another:
3
Bob Tarr
Design Patterns In Java
The Adapter Pattern
5
5
The Adapter Pattern
The Adapter Pattern
l
Structure
é
An object adapter relies on object composition:
Bob Tarr
Design Patterns In Java
The Adapter Pattern
6
6
The Adapter Pattern
The Adapter Pattern
l
Applicability
Use the Adapter pattern when
é
You want to use an existing class, and its interface does not match the one
you need
é
You want to create a reusable class that cooperates with unrelated classes
with incompatible interfaces
l
Implementation Issues
é
How much adapting should be done?
Ý
Simple interface conversion that just changes operation names and order of
arguments
Ý
Totally different set of operations
é
Does the adapter provide two-way transparency?
Ý
A two-way adapter supports both the Target and the Adaptee interface. It
allows an adapted object (Adapter) to appear as an Adaptee object or a Target
object
4
Bob Tarr
Design Patterns In Java
The Adapter Pattern
7
7
Adapter Pattern Example 1
Adapter Pattern Example 1
l
The classic round pegs and square pegs!
l
Here's the SquarePeg class:
/**
* The SquarePeg class.
* This is the Target class.
*/
public class SquarePeg {
public void insert(String str) {
System.out.println("SquarePeg insert(): " + str);
}
}
Bob Tarr
Design Patterns In Java
The Adapter Pattern
8
8
Adapter Pattern Example 1 (Continued)
Adapter Pattern Example 1 (Continued)
l
And the RoundPeg class:
/**
* The RoundPeg class.
* This is the Adaptee class.
*/
public class RoundPeg {
public void insertIntoHole(String msg) {
System.out.println("RoundPeg insertIntoHole(): " + msg);
}
}
l
If a client only understands the SquarePeg interface for inserting
pegs using the insert() method, how can it insert round pegs? A
peg adapter!
5
Bob Tarr
Design Patterns In Java
The Adapter Pattern
9
9
Adapter Pattern Example 1 (Continued)
Adapter Pattern Example 1 (Continued)
l
Here is the PegAdapter class:
/**
* The PegAdapter class.
* This is the Adapter class.
* It adapts a RoundPeg to a SquarePeg.
* Its interface is that of a SquarePeg.
*/
public class PegAdapter extends SquarePeg {
private RoundPeg roundPeg;
public PegAdapter(RoundPeg peg) {this.roundPeg = peg;}
public void insert(String str) {roundPeg.insertIntoHole(str);}
}
Bob Tarr
Design Patterns In Java
The Adapter Pattern
10
10
Adapter Pattern Example 1 (Continued)
Adapter Pattern Example 1 (Continued)
l
Typical client program:
// Test program for Pegs.
public class TestPegs {
public static void main(String args[]) {
// Create some pegs.
RoundPeg roundPeg = new RoundPeg();
SquarePeg squarePeg = new SquarePeg();
// Do an insert using the square peg.
squarePeg.insert("Inserting square peg...");
6
Bob Tarr
Design Patterns In Java
The Adapter Pattern
11
11
Adapter Pattern Example 1 (Continued)
Adapter Pattern Example 1 (Continued)
// Now we'd like to do an insert using the round peg.
// But this client only understands the insert()
// method of pegs, not a insertIntoHole() method.
// The solution: create an adapter that adapts
// a square peg to a round peg!
PegAdapter adapter = new PegAdapter(roundPeg);
adapter.insert("Inserting round peg...");
}
}
l
Client program output:
SquarePeg insert(): Inserting square peg...
RoundPeg insertIntoHole(): Inserting round peg...
Bob Tarr
Design Patterns In Java
The Adapter Pattern
12
12
Adapter Pattern Example 2
Adapter Pattern Example 2
l
Notice in Example 1 that the PegAdapter adapts a RoundPeg to a
SquarePeg. The interface for PegAdapter is that of a SquarePeg.
l
What if we want to have an adapter that acts as a SquarePeg or a
RoundPeg? Such an adapter is called a two-way adapter.
l
One way to implement two-way adapters is to use multiple
inheritance, but we can't do this in Java
l
But we can have our adapter class implement two different Java
interfaces!
7
Bob Tarr
Design Patterns In Java
The Adapter Pattern
13
13
Adapter Pattern Example 2 (Continued)
Adapter Pattern Example 2 (Continued)
l
Here are the interfaces for round and square pegs:
/**
*The IRoundPeg interface.
*/
public interface IRoundPeg {
public void insertIntoHole(String msg);
}
/**
*The ISquarePeg interface.
*/
public interface ISquarePeg {
public void insert(String str);
}
Bob Tarr
Design Patterns In Java
The Adapter Pattern
14
14
Adapter Pattern Example 2 (Continued)
Adapter Pattern Example 2 (Continued)
l
Here are the new RoundPeg and SquarePeg classes. These are
essentially the same as before except they now implement the
appropriate interface.
// The RoundPeg class.
public class RoundPeg implements IRoundPeg {
public void insertIntoHole(String msg) {
System.out.println("RoundPeg insertIntoHole(): " + msg);
}
}
// The SquarePeg class.
public class SquarePeg implements ISquarePeg {
public void insert(String str) {
System.out.println("SquarePeg insert(): " + str);
}
}
8
Bob Tarr
Design Patterns In Java
The Adapter Pattern
15
15
Adapter Pattern Example 2 (Continued)
Adapter Pattern Example 2 (Continued)
l
And here is the new PegAdapter:
/**
* The PegAdapter class.
* This is the two-way adapter class.
*/
public class PegAdapter implements ISquarePeg, IRoundPeg {
private RoundPeg roundPeg;
private SquarePeg squarePeg;
public PegAdapter(RoundPeg peg) {this.roundPeg = peg;}
public PegAdapter(SquarePeg peg) {this.squarePeg = peg;}
public void insert(String str) {roundPeg.insertIntoHole(str);}
public void insertIntoHole(String msg){squarePeg.insert(msg);}
}
Bob Tarr
Design Patterns In Java
The Adapter Pattern
16
16
Adapter Pattern Example 2 (Continued)
Adapter Pattern Example 2 (Continued)
l
A client that uses the two-way adapter:
// Test program for Pegs.
public class TestPegs {
public static void main(String args[]) {
// Create some pegs.
RoundPeg roundPeg = new RoundPeg();
SquarePeg squarePeg = new SquarePeg();
// Do an insert using the square peg.
squarePeg.insert("Inserting square peg...");
// Create a two-way adapter and do an insert with it.
ISquarePeg roundToSquare = new PegAdapter(roundPeg);
roundToSquare.insert("Inserting round peg...");
9
Bob Tarr
Design Patterns In Java
The Adapter Pattern
17
17
Adapter Pattern Example 2 (Continued)
Adapter Pattern Example 2 (Continued)
// Do an insert using the round peg.
roundPeg.insertIntoHole("Inserting round peg...");
// Create a two-way adapter and do an insert with it.
IRoundPeg squareToRound = new PegAdapter(squarePeg);
squareToRound.insertIntoHole("Inserting square peg...");
}
}
l
Client program output:
SquarePeg insert(): Inserting square peg...
RoundPeg insertIntoHole(): Inserting round peg...
RoundPeg insertIntoHole(): Inserting round peg...
SquarePeg insert(): Inserting square peg...
Bob Tarr
Design Patterns In Java
The Adapter Pattern
18
18
Adapter Pattern Example 3
Adapter Pattern Example 3
l
This example comes from Roger Whitney, San Diego State
University
l
Situation: A Java class library exists for creating CGI web server
programs. One class in the library is the CGIVariables class
which stores all CGI environment variables in a hash table and
allows access to them via a get(String evName) method. Many
Java CGI programs have been written using this library. The
latest version of the web server supports servlets, which provide
functionality similar to CGI programs, but are considerably more
efficient. The servlet library has an HttpServletRequest class
which has a getX() method for each CGI environment variable.
We want to use servlets. Should we rewrite all of our existing
Java CGI programs??
10
Bob Tarr
Design Patterns In Java
The Adapter Pattern
19
19
Adapter Pattern Example 3
Adapter Pattern Example 3
l
Solution : Well, we'll have to do some rewriting, but let's attempt
to m