- 介绍
桥接模式类似于策略模式,与策略模式不同之处是接口也可以发生改变。比如在windos下调用impl具体实现类、在linux下调用impl具体实现类。
- 类图
- 代码
public interface Implementor { public void operation(); } public class ConcreteImplementorA implements Implementor { public void operation() { System.out.println("ConcreteImplementorA"); } } public class ConcreteImplementorB implements Implementor { public void operation() { System.out.println("ConcreteImplementorB"); } } public abstract class Abstraction { private Implementor implementor; public Implementor getImplementor() { return implementor; } public void setImplementor(Implementor implementor) { this.implementor = implementor; } public abstract void operation(); } public class RefinedAbstraction extends Abstraction { public void operation() { super.getImplementor().operation(); } } public class Main { public static void main(String[] args) { Abstraction abstraction=new RefinedAbstraction(); abstraction.setImplementor(new ConcreteImplementorA()); abstraction.operation(); } }