Bridge模式简介
桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。这种模式涉及到一个作为桥接的接口,使得实体类的功能独立于接口实现类。这两种类型的类可被结构化改变而互不影响。
运用场景分析
设想如果要绘制矩形、圆形、椭圆、正方形,我们至少需要4个形状类,但是如果绘制的图形需要具有不同的颜色,如红色、绿色、蓝色等,此时至少有如下两种设计方案:
•第一种设计方案是为每一种形状都提供一套各种颜色的版本。
•第二种设计方案是根据实际需要对形状和颜色进行组合。
方案1:

方案2:

(图片来自与网络,顺路借用了图片相关的例子,图片出处Click Here!)
方法一具体分析
这个方法主要通过类的继承来实现不同颜色不类型图形的绘制
那么问题来了,如果我还需要添加一个图形,那么就会有另一种概念上的变化,会有5X3个图形的具体实例化对象(5种图形,3种颜色),如果在添加一种颜色,会有5X4个图形的具体实例化对象(5种图形,4种颜色),于是类的数量爆炸性增长情况出现了
同时这种办法还会带来以下问题吗?我们可以反问自己?
- 看上去是否存在冗余?
- 是高内聚的还是低内聚的?
- 是紧耦合还是松耦合的?
答案是显而易见的,存在大量的冗余代码,低内聚,紧耦合。
方法二具体分析
方法二将图形和图形具体颜色分别作为一个概念抽象出来,并通过类的组合来实现了图形与颜色的自由组合,达到了方法一的结果,设计方案二即是桥接模式的应用。桥接模式将继承关系转换为关联关系,将抽象与实现分离(这儿的实现不是抽象类的派生类,是抽象类及其派生类用来实现自己的对象),从而降低了类与类之间的耦合,减少了代码编写量,这样如果我再添加一个具体的图形或者颜色分类就只需要添加各自抽象类的分支就好了,也不会影响现有的其他实例类。
Bridge模式结构分析
1.抽象类(Abstraction):定义抽象类的接口,维护一个指向Implementor类型对象的指针
2.扩充抽象类(RefinedAbstraction):扩充由Abstraction定义的接口
3.实现类接口(Implementor):定义实现类的接口,该接口不一定要与Abstraction的接口完全一致;事实上这两个接口可以完全不同。一般来讲, Implementor接口仅提供基本操作,而 Abstraction则定义了基于这些基本操作的较高层次的操作。
4.具体实现类(ConcreteImplementor):实现Implementor接口并定义它的具体实现。
Bridge模式关键特征
1.意图:将抽象部分与实现部分分离,使它们都可以独立的变化。
2.主要解决:在有多种可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活。
3.何时使用:实现系统可能有多个角度分类,每一种角度都可能变化。
4.如何解决:把这种多角度分类分离出来,让它们独立变化,减少它们之间耦合。
5.关键代码:抽象类依赖实现类
6.应用实例: 1、猪八戒从天蓬元帅转世投胎到猪,转世投胎的机制将尘世划分为两个等级,即:灵魂和肉体,前者相当于抽象化,后者相当于实现化。生灵通过功能的委派,调用肉体对象的功能,使得生灵可以动态地选择。 2、墙上的开关,可以看到的开关是抽象的,不用管里面具体怎么实现的。
7.优点: 1、抽象和实现的分离。 2、优秀的扩展能力。 3、实现细节对客户透明。
8.缺点:桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程。
9.使用场景: 1、如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系,通过桥接模式可以使它们在抽象层建立一个关联关系。 2、对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用。 3、一个类存在两个独立变化的维度,且这两个维度都需要进行扩展。
10.注意事项:对于两个独立变化的维度,使用桥接模式再适合不过了。
具体实现
绘图程序的超级简化版本:利用程序画出红色或绿色的圆形或矩形。
Step1:创建桥接实现接口
IDrawing.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BridgePatternDemo_桥接模式_ { interface IDrawing { void DrawShape(); } } |
Step2:创建实现了IDrawing 接口的实体桥接实现类。
RedShapeDrawing.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BridgePatternDemo_桥接模式_ { class RedShapeDrawing : IDrawing { public void DrawShape() { Console.WriteLine("this shape's color is Red."); } } } |
GreenShapeDrawing.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BridgePatternDemo_桥接模式_ { class GreenShapeDrawing : IDrawing { public void DrawShape() { Console.WriteLine("this shape's color is Green."); } } } |
Step3:创建使用桥接接口(IDrawing )的抽象类 Shape
Shape.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BridgePatternDemo_桥接模式_ { abstract class Shape { protected IDrawing drawing; public Shape(IDrawing drawing) { this.drawing = drawing; } public abstract void Draw(); } } |
Step4:创建实现了 Shape 接口的实体类。
Circle.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BridgePatternDemo_桥接模式_ { class Circle : Shape { public Circle(IDrawing drawing) : base(drawing) { } public override void Draw() { Console.Write("Draw a Circle,"); drawing.DrawShape(); } } } |
Rectangle.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BridgePatternDemo_桥接模式_ { class Rectangle : Shape { public Rectangle(IDrawing drawing) : base(drawing) { } public override void Draw() { Console.Write("Draw a Rectangle,"); drawing.DrawShape(); } } } |
Step5:创建客户端使用 Shape 和 IDrawing 类画出不同颜色的圆。
Client.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BridgePatternDemo_桥接模式_ { class Client { static void Main(string[] args) { Shape redCircle = new Circle(new RedShapeDrawing()); redCircle.Draw(); Shape greenCicle = new Circle(new GreenShapeDrawing()); greenCicle.Draw(); Shape redRectangle = new Rectangle(new RedShapeDrawing()); redRectangle.Draw(); Shape greenRectangle = new Rectangle(new GreenShapeDrawing()); greenRectangle.Draw(); } } } |
输出结果
1 2 3 4 |
Draw a Circle,this shape's color is Red. Draw a Circle,this shape's color is Green. Draw a Rectangle,this shape's color is Red. Draw a Rectangle,this shape's color is Green. |