设计模式(一)之适配器模式

来源:互联网 发布:莎莎源码网 编辑:程序博客网 时间:2024/05/29 10:28

被适配的类:

public class Adaptee {    public void specificMethod(){        System.out.println("三脚插头");    }}

适配接口

public interface AppleInterface {    public void phoneInterface();}

适配器

public class Adapter implements AppleInterface {    private Adaptee adaptee;    public Adapter(Adaptee adaptee) {        this.adaptee = adaptee;    }    @Override    public void phoneInterface() {        adaptee.specificMethod();    }}

参照类

public class Linghtning implements AppleInterface {    @Override    public void phoneInterface() {        System.out.println("Linghtning.");    }}

调用者

public class Consumer {    public static void main(String[] args) {        // 适配到三脚插头        AppleInterface linghtning = new Adapter(new Adaptee());        linghtning.phoneInterface();        // 只是自己        linghtning = new Linghtning();        linghtning.phoneInterface();    }}
0 0
原创粉丝点击