结构图

模式说明
类设计
模式说明
- 扩展点在Adaptee类,Adaptee可以被其他类替换。
- 客户端需要知道哪个具体的Adapter负责将Adaptee转换成需要的Target接口。
- 客户端不关心具体哪个Adaptee被适配和转换的。
- 将一个类的接口转换成客户希望的另一个接口,适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
public static void main(String[] args) {
Target target = new Adapter();
target.request();
}
类设计
public interface Target {
public void request();
}
public class Adapter implements Target {
private Adaptee adaptee = new Adaptee();
@Override
public void request() {
adaptee.specificRequest();
}
}
public class Adaptee {
public void specificRequest() {
System.out.println("specific request");
}
}