Design Patterns - Adapter Pattern

adapter-pattern

Convert the interface of a class into another interface that the client expect. Adapter lets class work together that could not otherwise because of incompatibility interfaces. ~ Gang of Four

   
Intent Match an existing object beyond your control to particular interface
Problem Unable to use certain classes because of their interface mismatch
Solution The Adapter provides a wrapper with the desired interface
Participants and Collaborators The Adapter adapts the interface of an Adaptee to match that one of the Adapter’s Target, which allows the Client to use the Adaptee as it were a type of Target
Consequences The Adapter pattern allows for preexisting objects to fit into new class structure without being limited by their interface
Implementation Contain the existing class in another class. Have the containing class match the required interface and call the methods of the contained class

adapter-pattern

Summary

  • When you need to use an existing class and it’s interface is not the one you need, use Adapter
  • It is implemented by creating a new class with the desired interface and then wrap the original class methods to effectively contain the adapted object.
  • An adapter wraps an object to change its interface, a decorator wraps an object to add new behavior and responsibilities and facade wraps a set of objects to simply.
package com.art.head_first.turkeyadapter;
public interface Duck {
public void quack();
public void fly();
}
view raw Duck.java hosted with ❤ by GitHub
package com.art.head_first.turkeyadapter;
import org.junit.Test;
public class DuckTestDrive {
@Test
public void testTurkeyAdapter(){
MallardDuck duck = new MallardDuck();
WildTurkey turkey = new WildTurkey();
System.out.println("**************************************************");
System.out.println("Turkey Says: ");
turkey.gobble();
turkey.fly();
System.out.println("**************************************************");
System.out.println("Duck Says");
performDuckActions(duck);
System.out.println("**************************************************");
System.out.println("Turkey Adapter Says: ");
Duck turkeyAdapter = new TurkeyAdapter(turkey);
performDuckActions(turkeyAdapter);
}
private void performDuckActions(Duck duck){
duck.quack();
duck.fly();
}
}
package com.art.head_first.turkeyadapter;
public class MallardDuck implements Duck{
@Override
public void quack() {
System.out.println("Quack");
}
@Override
public void fly() {
System.out.println("I'm Flying");
}
}
package com.art.head_first.turkeyadapter;
public interface Turkey {
public void gobble();
public void fly();
}
view raw Turkey.java hosted with ❤ by GitHub
package com.art.head_first.turkeyadapter;
public class TurkeyAdapter implements Duck {
Turkey turkey;
public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
}
@Override
public void quack() {
turkey.gobble();
}
@Override
public void fly() {
for (int i=0; i<5; i++){
turkey.fly();
}
}
}
package com.art.head_first.turkeyadapter;
public class WildTurkey implements Turkey{
@Override
public void gobble() {
System.out.println("Gobble Gobble");
}
@Override
public void fly() {
System.out.println("I'm flying short distance");
}
}
view raw WildTurkey.java hosted with ❤ by GitHub