文章永久连接:https://tech.souyunku.com/2673
桥接模式(Bridge Pattern)是用于把抽象化与实现化解耦,使得二者可以独立变化
桥接模式涉及到一个作为桥接的接口,使得实体类的功能独立于接口实现类,这两种类型的类可被结构化改变而互不影响
桥接模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦
摘要
1、意图:
将抽象部分与实现部分分离,使它们都可以独立的变化
2、主要解决:
在有多种可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活
3、何时使用:
实现系统可能有多个角度分类,每一种角度都可能变化
4、如何解决:
把这种多角度分类分离出来,让它们独立变化,减少它们之间耦合
5、关键代码:
抽象类依赖实现类
6、应用实例:
1、猪八戒从天蓬元帅转世投胎到猪,转世投胎的机制将尘世划分为两个等级,即:灵魂和肉体,前者相当于抽象化,后者相当于实现化
生灵通过功能的委派,调用肉体对象的功能,使得生灵可以动态地选择
2、墙上的开关,可以看到的开关是抽象的,不用管里面具体怎么实现的
7、优点:
1、抽象和实现的分离
2、优秀的扩展能力
3、实现细节对客户透明
8、缺点:
桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程
9、使用场景:
1、如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系,通过桥接模式可以使它们在抽象层建立一个关联关系
2、对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用
3、一个类存在两个独立变化的维度,且这两个维度都需要进行扩展
10、 注意事项:
对于两个独立变化的维度,使用桥接模式再适合不过了
实现
1、创建一个作为桥接实现的 DrawAPI 接口和实现了 DrawAPI 接口的实体类 RedCircle 、 GreenCircle
2、Shape 是一个抽象类,将使用 DrawAPI 的对象
3、BridgePatternDemo 使用 Shape 类来画出不同颜色的圆
范例
我们通过下面的实例来演示桥接模式(Bridge Pattern)的用法: 可以使用相同的抽象类方法但是不同的桥接实现类,来画出不同颜色的圆
1. 创建桥接实现接口
DrawAPI.java
// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
package com.souyunku.tech.gof;
public interface DrawAPI {
public void drawCircle(int radius, int x, int y);
}
2. 创建实现了 DrawAPI 接口的实体桥接实现类
RedCircle.java
// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
package com.souyunku.tech.gof;
public class RedCircle implements DrawAPI {
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: red, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
GreenCircle.java
// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
package com.souyunku.tech.gof;
public class GreenCircle implements DrawAPI {
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: green, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
3. 使用 DrawAPI 接口创建抽象类 Shape
Shape.java
// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
package com.souyunku.tech.gof;
public abstract class Shape {
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawAPI = drawAPI;
}
public abstract void draw();
}
4. 创建实现了 Shape 接口的实体类
Circle.java
// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
package com.souyunku.tech.gof;
public class Circle extends Shape {
private int x, y, radius;
public Circle(int x, int y, int radius, DrawAPI drawAPI) {
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}
5. 使用 Shape 和 DrawAPI 类画出不同颜色的圆
BridgePatternDemo.java
// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
package com.souyunku.tech.gof;
public class BridgePatternDemo {
public static void main(String[] args) {
Shape redCircle = new Circle(100,100, 10, new RedCircle());
Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
redCircle.draw();
greenCircle.draw();
}
}
编译运行以上 Java 范例,输出结果如下
$ javac -d . src/main/com/souyunku/tech/gof/BridgePatternDemo.java
$ java com.souyunku.tech.gof.BridgePatternDemo
Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]
干货推荐
附录:设计模式:系列文章
- 一、设计模式
- 二、设计模式 – 简介
- 三、设计模式 – 四大类型
- 四、设计模式 – 六大原则
- 五、工厂模式 ( Factory Pattern )
- 六、抽象工厂模式 ( Abstract Factory Pattern )
- 七、单例模式 ( Singleton Pattern )
- 八、建造者模式 ( Builder Pattern )
- 九、原型模式 ( Prototype Pattern )
- 十、适配器模式 ( Adapter Pattern )
- 【当前读到】十一、桥接模式 ( Bridge Pattern )
- 十二、过滤器模式 ( Filter Pattern )
- 十三、组合模式 ( Composite Pattern )
- 十四、装饰器模式 ( Decorator Pattern )
- 十五、外观模式 ( Facade Pattern )
- 十六、享元模式 ( Flyweight Pattern )
- 十七、代理模式 ( Proxy Pattern )
- 十八、责任链模式 ( Chain of Responsibility)
- 十九、命令模式 ( Command Pattern )
- 二十、解释器模式 ( Interpreter Pattern )
- 二十一、迭代器模式 ( Iterator Pattern )
- 二十二、中介者模式 ( Mediator Pattern )
- 二十三、备忘录模式 ( Memento Pattern )
- 二十四、观察者模式 ( Observer Pattern )
- 二十五、状态模式 ( State Pattern )
- 二十六、空对象模式 ( Null Object Pattern )
- 二十七、策略模式 ( Strategy Pattern )
- 二十八、模板模式 ( Template Pattern )
- 二十九、访问者模式 ( Visitor Pattern )
- 三十、MVC 模式
- 三十一、业务代表模式(Business Delegate Pattern)
- 三十二、组合实体模式 (Composite Entity Pattern)
- 三十三、数据访问对象模式 ( Data Access Object )
- 三十四、前端控制器模式(Front Controller Pattern)
- 三十五、拦截过滤器模式 ( Intercepting Filter )
- 三十六、服务定位器模式 (Service Locator Pattern)
- 三十七、传输对象模式 ( Transfer Object Pattern )
- 三十八、设计模式资源