您的位置:首页 > 其它

设计模式(12)桥接模式

2019-01-26 11:23 183 查看

定义:处理多层继承结构,处理多维度变化的场景,将各个维度设计成独立 的继承结构,使各个维度可以独立的扩展在抽象层建立关联。

应用场景:

就是搭建一个桥,让两个组件之间互相调用,可以通过桥来执行。因为有了桥,两个代码组件无论怎么修改,都互相没有影响。这个桥,实际上就是一个接口。所以说,java中,无处不桥接,只要你是面向接口编程,基本就是在桥接。

public class BridgePatternDemo {

public static void main(String[] args) {
Implementor implementor = new ConcreteImplementor();
Abstraction abstraction = new RefinedAbstraction(implementor);
abstraction.execute();
}

public interface Implementor {
void execute();
}

public static class ConcreteImplementor implements Implementor {

@Override
public void execute() {
System.out.println("执行了功能逻辑");
}

}

public static abstract class Abstraction {

protected Implementor implementor;

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

public abstract void execute();

}

public static class RefinedAbstraction extends Abstraction {

public RefinedAbstraction(Implementor implementor) {
super(implementor);
}

@Override
public void execute() {
implementor.execute();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: