The adapter does this by taking an instance of the class to be converted (the adaptee) and uses the methods the adaptee has available to create the methods which are expected.
In this example we have a TeaBall class which takes in an instance of LooseLeafTea. The TeaBall class uses the steepTea method from LooseLeafTea and adapts it to provide the steepTeaInCup method which the TeaCup class requires.
TeaBag.java - the class which the adapter will make the adaptee adapt to
public class TeaBag {
boolean teaBagIsSteeped;
public TeaBag() {
teaBagIsSteeped = false;
}
public void steepTeaInCup() {
teaBagIsSteeped = true;
System.out.println("tea bag is steeping in cup");
}
}
download source, use right-click and "Save Target As..." to save with a .java extension.
TeaBall.java - the adapter
public class TeaBall extends TeaBag {
LooseLeafTea looseLeafTea;
public TeaBall(LooseLeafTea looseLeafTeaIn) {
looseLeafTea = looseLeafTeaIn;
teaBagIsSteeped = looseLeafTea.teaIsSteeped;
}
public void steepTeaInCup() {
looseLeafTea.steepTea();
teaBagIsSteeped = true;
}
}
download source, use right-click and "Save Target As..." to save with a .java extension.
LooseLeafTea.java - the adaptee
public class LooseLeafTea {
boolean teaIsSteeped;
public LooseLeafTea() {
teaIsSteeped = false;
}
public void steepTea() {
teaIsSteeped = true;
System.out.println("tea is steeping");
}
}
download source, use right-click and "Save Target As..." to save with a .java extension.
TeaCup.java - the class that accepts class TeaBag in it's steepTeaBag() method, and so is being adapted for.
public class TeaCup {
public void steepTeaBag(TeaBag teaBag) {
teaBag.steepTeaInCup();
}
}
download source, use right-click and "Save Target As..." to save with a .java extension.
TestTeaBagAdaptation.java - testing the adapter
class TestTeaBagAdaptation {
public static void main(String[] args) {
TeaCup teaCup = new TeaCup();
System.out.println("Steeping tea bag");
TeaBag teaBag = new TeaBag();
teaCup.steepTeaBag(teaBag);
System.out.println("Steeping loose leaf tea");
LooseLeafTea looseLeafTea = new LooseLeafTea();
TeaBall teaBall = new TeaBall(looseLeafTea);
teaCup.steepTeaBag(teaBall);
}
}
download source, use right-click and "Save Target As..." to save with a .java extension.
Test Results
Steeping tea bag tea bag is steeping in cup Steeping loose leaf tea tea is steeping
Notes
The basic premise of the adapter is that you either can not or do not want to change the adaptee. This might be because you purchased the adaptee, and do not have the source code.
There are two GoF versions of the adapter.
The First is the inheriting version, in which the adapter inherits from both "the adaptee" and "the class that adapter will make the adaptee adapt to".
The Second is the object version, which is shown here.
Reference Design Patterns pages 139-141.
There are two GoF versions of the adapter.
The First is the inheriting version, in which the adapter inherits from both "the adaptee" and "the class that adapter will make the adaptee adapt to".
The Second is the object version, which is shown here.
Reference Design Patterns pages 139-141.
UML
UML for AdapterReferences
online
Portland Pattern RepositoryBooks
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John VlissidesJava Design Patterns - A Tutorial by James W. Cooper