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(); } }