编程语言
首页 > 编程语言> > 基于JAVA的设计模式之桥接模式

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

标签:Implementor,JAVA,桥接,void,class,implementor,operation,设计模式,public
来源: https://www.cnblogs.com/hbsdljz/p/11131459.html