接下来的两个模式目的就是:Connecting different types
Adapter takes one type and produces an interface to some other type. When you’ve got this , and you need that , Adapter solves the problem. The only requirement is to produce a that , and there are a number of ways you can accomplish this adaptation.
//: adapter:SimpleAdapter.java
// "Object Adapter" from GoF diagram
package adapter;
import junit.framework.*;
class Target {
public void request() {}
}
class Adaptee {
public void specificRequest() {
System.out.println("Adaptee: SpecificRequest");
}
}
class Adapter extends Target {
private Adaptee adaptee;
public Adapter(Adaptee a) {
adaptee = a;
}
public void request() {
adaptee.specificRequest();
}
}
public class SimpleAdapter extends TestCase {
Adaptee a = new Adaptee();
Target t = new Adapter(a);
public void test() {
t.request();
}
public static void main(String args[]) {
junit.textui.TestRunner.run(SimpleAdapter.class);
}
} ///:~
......
正如作者论述的:
I’m taking liberties with the term “proxy” here, because in Design Patterns they assert that a proxy must have an identical interface with the object that it is a surrogate for. However, if you have the two words together: “proxy adapter,” it is perhaps more reasonable.
Adapter设计模式在格式上是和Proxy有很大相似之处.都是很好地应用了组合或者继承构造了新类。这个在实际应用当中就能体会到,总是看简单样例,感觉没有什么区别....继续前行!