大话设计模式-适配器模式

版权声明:Leo.All Rights Reserved. https://blog.csdn.net/qq_41113081/article/details/89705070

在这里插入图片描述

需要适配的类

/**
 * 需要适配的类
 */
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();
    }
}
//运行结果: 
//适配器适配的接口: 
//特殊的请求(这是需要适配的方法)

猜你喜欢

转载自blog.csdn.net/qq_41113081/article/details/89705070