23种设计模式之桥接模式

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

 

package designMode.bridge;

interface Implementor {

    void operation();
}
package designMode.bridge;

class ConcreteImplementorA implements Implementor{

    @Override
    public void operation() {
        System.out.println("具体实现A的方法执行");
    }
}
class ConcreteImplementorB implements Implementor{
    
    @Override
    public void operation() {
        System.out.println("具体实现B的方法执行");
    }
}
package designMode.bridge;

class Abstraction {
    
    protected Implementor implementor;

    public void setImplementor(Implementor implementor) {
        this.implementor = implementor;
    }
    
    public void operation(){
        implementor.operation();
    }
}
package designMode.bridge;

class RefinedAbstraction extends Abstraction{

    @Override
    public void operation() {
        super.operation();
    }
}
package designMode.bridge;

class Test {
    public static void main(String[] args) {
        Abstraction ab = new RefinedAbstraction();
        ab.setImplementor(new ConcreteImplementorA());
        ab.operation();
        ab.setImplementor(new ConcreteImplementorA());
        ab.operation();
    }
}

 

posted @ 2017-08-23 14:39  如果屈原会编程  阅读(188)  评论(0编辑  收藏  举报