Adapter Design Patterns
Adapter Design Patterns
Lecture 21
Adapter Design Pattern
Gang of Four state the intent of Adapter is to
Convert the interface of a class into another interface that
the clients expect. Adapter lets classes work together that
could not otherwise because of incompatible interfaces.
Problem Specification:
Step 1:
You are given the following class library to draw
shapes.
Shape
+display()
+fill()
+undisplay()
Line Square
+display() +display()
+fill() +fill()
+undisplay() +undisplay()
Step 2:
+display()
+fill()
+undisplay()
Circle
Line Square
+display() +display()
+fill() +display()
+fill() +fill()
+undisplay() +undisplay() +undisplay()
Step 3:
Then your client said: “No,no,no”. You have to use this
nice class library for drawing circles. Unfortunately, it is
from a different vendor and it has a different interface.
Shape
+display()
+fill()
AnotherCircle
+undisplay()
+setLocation()
+drawIt()
+fillIt()
+setItColor()
Line Square +unDrawIt()
+display() +display()
+fill() +fill()
+undisplay() +undisplay()
Adapter Design Pattern
Problem: An "off the shelf" component offers
compelling functionality that you would like to reuse,
but its "view of the world" is not compatible with the
philosophy and architecture of the new system
Adapter is about creating an intermediary abstraction
that translates, or maps, the old component to the new
system
Use Adapter when you need a way to create a new
interface for an object that does the right stuff but has
the wrong interface
Two kinds of adapter pattern
AbstractClass
AdapterClass AdapteeClass
Adapter Design Pattern Solution
Object Adapter
Shape
+display()
adaptee
+fill()
+undisplay()
AnotherCircle
Line Circle
Square
AbstractClass AdapteeClass
AdapterClass
Adapter Design Pattern Solution
Class Adapter
AnotherCircle
Shape
+setLocation()
+drawIt()
+display() +fillIt()
+fill() +setItColor()
+undisplay() +unDrawIt()
+display() +display()
+fill() +fill() +display()
+undisplay() +undisplay() +fill()
+undisplay()
Class Adapter
uses inheritance and can only wrap a class. It cannot
wrap an interface since by definition it must derive
from some base class.
Object Adapter
uses composition and can wrap classes or interfaces, or
both. It can do this since it contains, as a private,
encapsulated member, the class or interface object
instance it wraps.
Goal of Adapter Pattern
}
Step 2:
// EnemyTank implements EnemyAttacker perfectly
// Our job is to make classes with different methods
// from EnemyAttacker to work with the EnemyAttacker interface
import java.util.Random;
public class EnemyTank implements EnemyAttacker{
Random generator = new Random();
public void fireWeapon() {
int attackDamage = generator.nextInt(10) + 1;
System.out.println("Enemy Tank Does " + attackDamage + " Damage");}
public void driveForward() {
int movement = generator.nextInt(5) + 1;
System.out.println("Enemy Tank moves " + movement + " spaces");}
public void assignDriver(String driverName) {
System.out.println(driverName + " is driving the tank"); }
}
Step 3: Adaptee
// This is the Adaptee. The Adapter sends method calls
// to objects that use the EnemyAttacker interface
// to the right methods defined in EnemyRobot
import java.util.Random;
public class EnemyRobot{
Random generator = new Random();
public void smashWithHands() {
int attackDamage = generator.nextInt(10) + 1;
System.out.println("Enemy Robot Causes " + attackDamage + " Damage
With Its Hands"); }
public void walkForward() {
int movement = generator.nextInt(5) + 1;
System.out.println("Enemy Robot Walks Forward " + movement + "
spaces"); }
public void reactToHuman(String driverName) {
System.out.println("Enemy Robot Tramps on " + driverName);
}
Step 4:Adapter
// The Adapter must provide an alternative action for
// the the methods that need to be used because
// EnemyAttacker was implemented.
theRobot.reactToHuman(driverName);
}
Step 5: Client
public class TestEnemyAttackers{
public static void main(String[] args){
EnemyTank rx7Tank = new EnemyTank();
EnemyRobot fredTheRobot = new EnemyRobot();
EnemyAttacker robotAdapter = new EnemyRobotAdapter(fredTheRobot);
System.out.println("The Robot");
fredTheRobot.reactToHuman("Paul");
fredTheRobot.walkForward();
fredTheRobot.smashWithHands();
System.out.println();
System.out.println("The Enemy Tank");
rx7Tank.assignDriver("Frank");
rx7Tank.driveForward();
rx7Tank.fireWeapon();
System.out.println();
Step 5: Client
System.out.println("The Robot with Adapter");
robotAdapter.assignDriver("Mark");
robotAdapter.driveForward();
robotAdapter.fireWeapon();
}
Output