大话设计模式-适配器模式
需要适配的类
/** * 需要适配的类 */
public class Adaptee {
public void SpecificRequest() {
System.out.println("特殊的请求(这是需要适配的方法)");
}
}
客户端所期待的接口
/** * 客户端所期待的接口 */
public interface Target {
public void Request();
}
适配器
/** * 适配器 */
public class Adapter implements Target {
private Adaptee adaptee = new Adaptee();
@Override
public void Request() {
System.out.println("适配器适配的接口: ");
adaptee.SpecificRequest();
}
}
测试主类
/** * 对象适配器模式测试类 */
public class Main {
public static void main(String[] args) {
Target target = new Adapter();
target.Request();
}
}
//运行结果:
//适配器适配的接口:
//特殊的请求(这是需要适配的方法)