桥接模式

一、定义

桥接模式(Bridge):将抽象部分与它的实现部分分离,使他们都可以独立的变化。

二、示例代码

public abstract class Abstraction {
    
    Implementor implementor;


    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }

    public  void operation(){
        implementor.operation();
    }
}

public class RefinedAbstraction extends Abstraction {

    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }
    public void operation(){
        System.out.println("调用自己特定的操作");
        implementor.operation();
    }
}

public abstract class Implementor {
    public abstract void operation();
}

public class ConcreteImplementorA extends Implementor {
    @Override
    public void operation() {
        System.out.println("具体方法A的执行");
    }
}

public class ConcreImplementorB extends Implementor {
    @Override
    public void operation() {
        System.out.println("具体方法B的执行");
    }
}

public class Client {
    public static void main(String[] args) {
        Implementor implementorA=new ConcreteImplementorA();
        Implementor implementorB=new ConcreImplementorB();
        Abstraction abstractionA=new RefinedAbstraction(implementorA);
        Abstraction abstractionB=new RefinedAbstraction(implementorB);
        abstractionA.operation();
        abstractionB.operation();
    }
}

posted @ 2020-01-26 22:25  兮云飞扬  阅读(70)  评论(0编辑  收藏  举报