23种设计模式学习之桥接模式

接口

public interface Source {
     void method1();
}

 实现一

public class SourceImp implements Source{

    @Override
    public void method1() {
        System.out.println("1111111");
    }
}

 实现二

public class SourceImpl implements Source {
    @Override
    public void method1() {
        System.out.println("2222");
    }
}

 定义桥

public abstract class Bridge {
    private Source source;
    public void method(){
        source.method1();
    }
    public Source getSource(){
        return source;
    }
    public void setSource(Source source){
        this.source=source;
    }

}

 桥

public class BridgeIn extends Bridge {
    @Override
    public void method(){
        this.getSource().method1();
    }

}

 实例

public class Demo {
    public static void main(String[] args) {
        Source source =new SourceImp();
        Source source1=new SourceImpl();
        BridgeIn bridgeIn=new BridgeIn();
        bridgeIn.setSource(source);
        bridgeIn.getSource().method1();
        bridgeIn.setSource(source1);
        bridgeIn.getSource().method1();
    }
}

 

posted @ 2018-01-16 16:05  我_会飞的鱼  阅读(134)  评论(0编辑  收藏  举报