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

需要适配的类

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

相关推荐

烤点老白薯:可以 除了名字都偷了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务