Java Design Patterns .

Java Design Patterns Adapter

Adapter (aka Wrapper) Overview

In the Adapter Design Pattern, a class converts the interface of one class to be what another class expects.

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");
   }
}
To download source right-click here and "Save As...".


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;
   }
}
To download source right-click here and "Save As...".


LooseLeafTea.java - the adaptee

public class LooseLeafTea {  
   boolean teaIsSteeped; 
    
   public LooseLeafTea() {
       teaIsSteeped = false;
   }
   
   public void steepTea() {
       teaIsSteeped = true;
       System.out.println("tea is steeping");
   }
}
To download source right-click here and "Save As...".


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();
   }
}
To download source right-click here and "Save As...".


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); } }
To download source right-click here and "Save As...".


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.

UML

UML for Adapter

References


online
Portland Pattern Repository
Books
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Java Design Patterns - A Tutorial by James W. Cooper
Comments
Sign in to be the first to comment on Java Design Patterns Adapter.