一、
1.
2.The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces
3.
4.The Adapter Pattern is full of good OO design principles: check out the use of object composition to wrap the adaptee with an altered interface. This approach has the added advantage that we can use an adapter with any subclass of the adaptee.
Also check out how the pattern binds the client to an interface, not an implementation; we could use several adapters, each converting a different backend set of classes. Or, we could add new implementations after the fact, as long as they
adhere to the Target interface.
5.
6.
二、duck假装是turkey,turkey假装是duck
1.
1 package headfirst.designpatterns.adapter.ducks; 2 3 public interface Duck { 4 public void quack(); 5 public void fly(); 6 }
2.
1 package headfirst.designpatterns.adapter.ducks; 2 3 public interface Turkey { 4 public void gobble(); 5 public void fly(); 6 }
3.
1 package headfirst.designpatterns.adapter.ducks; 2 3 public class MallardDuck implements Duck { 4 public void quack() { 5 System.out.println("Quack"); 6 } 7 8 public void fly() { 9 System.out.println("I'm flying"); 10 } 11 }
4.
1 package headfirst.designpatterns.adapter.ducks; 2 3 public class WildTurkey implements Turkey { 4 public void gobble() { 5 System.out.println("Gobble gobble"); 6 } 7 8 public void fly() { 9 System.out.println("I'm flying a short distance"); 10 } 11 }
5.
1 package headfirst.designpatterns.adapter.ducks; 2 import java.util.Random; 3 4 public class DuckAdapter implements Turkey { 5 Duck duck; 6 Random rand; 7 8 public DuckAdapter(Duck duck) { 9 this.duck = duck; 10 rand = new Random(); 11 } 12 13 public void gobble() { 14 duck.quack(); 15 } 16 17 public void fly() { 18 if (rand.nextInt(5) == 0) { 19 duck.fly(); 20 } 21 } 22 }
6.
1 package headfirst.designpatterns.adapter.ducks; 2 3 public class DuckTestDrive { 4 public static void main(String[] args) { 5 MallardDuck duck = new MallardDuck(); 6 7 WildTurkey turkey = new WildTurkey(); 8 Duck turkeyAdapter = new TurkeyAdapter(turkey); 9 10 System.out.println("The Turkey says..."); 11 turkey.gobble(); 12 turkey.fly(); 13 14 System.out.println(" The Duck says..."); 15 testDuck(duck); 16 17 System.out.println(" The TurkeyAdapter says..."); 18 testDuck(turkeyAdapter); 19 } 20 21 static void testDuck(Duck duck) { 22 duck.quack(); 23 duck.fly(); 24 } 25 }
7.
1 package headfirst.designpatterns.adapter.ducks; 2 3 public class TurkeyAdapter implements Duck { 4 Turkey turkey; 5 6 public TurkeyAdapter(Turkey turkey) { 7 this.turkey = turkey; 8 } 9 10 public void quack() { 11 turkey.gobble(); 12 } 13 14 public void fly() { 15 for(int i=0; i < 5; i++) { 16 turkey.fly(); 17 } 18 } 19 }
8.
1 package headfirst.designpatterns.adapter.ducks; 2 3 public class TurkeyTestDrive { 4 public static void main(String[] args) { 5 MallardDuck duck = new MallardDuck(); 6 Turkey duckAdapter = new DuckAdapter(duck); 7 8 for(int i=0;i<10;i++) { 9 System.out.println("The DuckAdapter says..."); 10 duckAdapter.gobble(); 11 duckAdapter.fly(); 12 } 13 } 14 }
三、Java用Iterator取代Enumeration,处理旧代码可以用适配器模式
While Java has gone in the direction of the Iterator, there is nevertheless a lot of legacy client code that depends on the Enumeration interface, so an Adapter that converts an Iterator to an Enumeration is also quite useful.
1.
2.
1 package headfirst.designpatterns.adapter.iterenum; 2 3 import java.util.*; 4 5 public class EnumerationIterator implements Iterator<Object> { 6 Enumeration<?> enumeration; 7 8 public EnumerationIterator(Enumeration<?> enumeration) { 9 this.enumeration = enumeration; 10 } 11 12 public boolean hasNext() { 13 return enumeration.hasMoreElements(); 14 } 15 16 public Object next() { 17 return enumeration.nextElement(); 18 } 19 20 public void remove() { 21 throw new UnsupportedOperationException(); 22 } 23 }
3.
1 package headfirst.designpatterns.adapter.iterenum; 2 3 import java.util.*; 4 5 public class IteratorEnumeration implements Enumeration<Object> { 6 Iterator<?> iterator; 7 8 public IteratorEnumeration(Iterator<?> iterator) { 9 this.iterator = iterator; 10 } 11 12 public boolean hasMoreElements() { 13 return iterator.hasNext(); 14 } 15 16 public Object nextElement() { 17 return iterator.next(); 18 } 19 }
4.
1 package headfirst.designpatterns.adapter.iterenum; 2 3 import java.util.*; 4 5 public class EnumerationIteratorTestDrive { 6 public static void main (String args[]) { 7 Vector<String> v = new Vector<String>(Arrays.asList(args)); 8 Iterator<?> iterator = new EnumerationIterator(v.elements()); 9 while (iterator.hasNext()) { 10 System.out.println(iterator.next()); 11 } 12 } 13 }
5.
1 package headfirst.designpatterns.adapter.iterenum; 2 3 import java.util.*; 4 5 public class IteratorEnumerationTestDrive { 6 public static void main (String args[]) { 7 ArrayList<String> l = new ArrayList<String>(Arrays.asList(args)); 8 Enumeration<?> enumeration = new IteratorEnumeration(l.iterator()); 9 while (enumeration.hasMoreElements()) { 10 System.out.println(enumeration.nextElement()); 11 } 12 } 13 }
6.