一、
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.
package headfirst.designpatterns.adapter.ducks; public interface Duck {
public void quack();
public void fly();
}
2.
package headfirst.designpatterns.adapter.ducks; public interface Turkey {
public void gobble();
public void fly();
}
3.
package headfirst.designpatterns.adapter.ducks; public class MallardDuck implements Duck {
public void quack() {
System.out.println("Quack");
} public void fly() {
System.out.println("I'm flying");
}
}
4.
package headfirst.designpatterns.adapter.ducks; public class WildTurkey implements Turkey {
public void gobble() {
System.out.println("Gobble gobble");
} public void fly() {
System.out.println("I'm flying a short distance");
}
}
5.
package headfirst.designpatterns.adapter.ducks;
import java.util.Random; public class DuckAdapter implements Turkey {
Duck duck;
Random rand; public DuckAdapter(Duck duck) {
this.duck = duck;
rand = new Random();
} public void gobble() {
duck.quack();
} public void fly() {
if (rand.nextInt(5) == 0) {
duck.fly();
}
}
}
6.
package headfirst.designpatterns.adapter.ducks; public class DuckTestDrive {
public static void main(String[] args) {
MallardDuck duck = new MallardDuck(); WildTurkey turkey = new WildTurkey();
Duck turkeyAdapter = new TurkeyAdapter(turkey); System.out.println("The Turkey says...");
turkey.gobble();
turkey.fly(); System.out.println("\nThe Duck says...");
testDuck(duck); System.out.println("\nThe TurkeyAdapter says...");
testDuck(turkeyAdapter);
} static void testDuck(Duck duck) {
duck.quack();
duck.fly();
}
}
7.
package headfirst.designpatterns.adapter.ducks; public class TurkeyAdapter implements Duck {
Turkey turkey; public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
} public void quack() {
turkey.gobble();
} public void fly() {
for(int i=0; i < 5; i++) {
turkey.fly();
}
}
}
8.
package headfirst.designpatterns.adapter.ducks; public class TurkeyTestDrive {
public static void main(String[] args) {
MallardDuck duck = new MallardDuck();
Turkey duckAdapter = new DuckAdapter(duck); for(int i=0;i<10;i++) {
System.out.println("The DuckAdapter says...");
duckAdapter.gobble();
duckAdapter.fly();
}
}
}
三、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.
package headfirst.designpatterns.adapter.iterenum; import java.util.*; public class EnumerationIterator implements Iterator<Object> {
Enumeration<?> enumeration; public EnumerationIterator(Enumeration<?> enumeration) {
this.enumeration = enumeration;
} public boolean hasNext() {
return enumeration.hasMoreElements();
} public Object next() {
return enumeration.nextElement();
} public void remove() {
throw new UnsupportedOperationException();
}
}
3.
package headfirst.designpatterns.adapter.iterenum; import java.util.*; public class IteratorEnumeration implements Enumeration<Object> {
Iterator<?> iterator; public IteratorEnumeration(Iterator<?> iterator) {
this.iterator = iterator;
} public boolean hasMoreElements() {
return iterator.hasNext();
} public Object nextElement() {
return iterator.next();
}
}
4.
package headfirst.designpatterns.adapter.iterenum; import java.util.*; public class EnumerationIteratorTestDrive {
public static void main (String args[]) {
Vector<String> v = new Vector<String>(Arrays.asList(args));
Iterator<?> iterator = new EnumerationIterator(v.elements());
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
5.
package headfirst.designpatterns.adapter.iterenum; import java.util.*; public class IteratorEnumerationTestDrive {
public static void main (String args[]) {
ArrayList<String> l = new ArrayList<String>(Arrays.asList(args));
Enumeration<?> enumeration = new IteratorEnumeration(l.iterator());
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
}
6.