GOF——适配器模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38358430/article/details/85256467

将一个类的接口转换成另外一个需要的接口

/**
 * 适配器模式
 */
interface Target {
    void handleReq();
}

/**
 * 被适配类
 */
class Adapted {
    public void request() {
        System.out.println("Adapted-->request");
    }
}

class Adaptive implements Target {

    private Adapted adapted;

    public Adaptive(Adapted adapted) {
        this.adapted = adapted;
    }

    @Override
    public void handleReq() {
        adapted.request();
    }
}
/**
 * 适配器模式
 */
public class Adapter {

    public void test1(Target t) {
        t.handleReq();
    }

    public static void main(String[] args) {

        Target target = new Adaptive(new Adapted());

        Adapter adapter = new Adapter();

        adapter.test1(target);

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38358430/article/details/85256467