基于JAVA的设计模式之桥接模式

  • 介绍

    桥接模式类似于策略模式,与策略模式不同之处是接口也可以发生改变。比如在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();
    }
}
posted @ 2019-07-04 11:37  Java不如烤咪。  阅读(175)  评论(0)    收藏  举报