设计模式-适配器模式

适配器模式:

将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以工作。

/**
 * 适配器模式,组合
 */
public class AdapterTest {
    public static void main(String[] args) {
        Target target = new Adapter(new Adaptee());
        target.output5v();
    }
}

class Adaptee{
    public int output220v(){
        return 220;
    }
}

interface Target{
    int output5v();
}

class Adapter implements Target {
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public int output5v() {
        int i = adaptee.output220v();
        //....
        System.out.println(String.format("原始电压:%d v   - >  输出电压: %d v  ",i,5));
        return 5;
    }
}
原文地址:https://www.cnblogs.com/chenfx/p/14788054.html