学习设计模式也有一段时间了,今天就把我整理的一篇课程和大家分享,有不妥之处欢迎指出.
生活中的一个例子:
就拿汽车在路上行驶的来说。即有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶。这你会发现,对于交通工具(汽车)有不同的类型,然而它们所行驶的环境(路)也在变化,在软件系统中就要适应两个方面的变化?怎样实现才能应对这种变化呢?
概述:
在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就要使用Bridge模式。
意图:
将抽象部分与实现部分分离,使它们都可以独立的变化。
——《设计模式》GOF
结构图:
传统的做法:
通过类继承的方式来做上面的例子;
先看一下类结构图:
代码实现:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
2
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
4
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
7
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
15
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
20
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
21
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
23
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
25
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
28
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
29
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
31
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
33
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
36
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
37
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
39
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
41
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
44
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
45
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
46
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
47
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
48
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
49
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
50
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
52
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
53
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
54
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
55
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
56
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
57
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
60
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
客户端调用:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
2
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
4
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
6
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
7
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
8
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
10
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
14
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
缺点:
但是我们说这样的设计是脆弱的,仔细分析就可以发现,它还是存在很多问题,首先它在遵循开放-封闭原则的同时,违背了类的单一职责原则,即一个类只有一个引起它变化的原因,而这里引起变化的原因却有两个,即路类型的变化和汽车类型的变化;其次是重复代码会很多,不同的汽车在不同的路上行驶也会有一部分的代码是相同的;再次是类的结构过于复杂,继承关系太多,难于维护,最后最致命的一点是扩展性太差。如果变化沿着汽车的类型和不同的道路两个方向变化,我们会看到这个类的结构会迅速的变庞大。
应用设计模式
桥接模式(Bridge)来做;
先看一下类结构图:
代码实现:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
2
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
4
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
6
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
8
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
20
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
21
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
23
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
25
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
26
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
29
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
30
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
31
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
33
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
35
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
36
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
2
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
4
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
7
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
10
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
15
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
18
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
20
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
22
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
24
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
客户端调用:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
2
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
4
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
6
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
7
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
8
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
10
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
14
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
15
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
可以看到,通过对象组合的方式,Bridge 模式把两个角色之间的继承关系改为了耦合的关系,从而使这两者可以从容自若的各自独立的变化,这也是Bridge模式的本意。
这样增加了客户程序与路与汽车的耦合。其实这样的担心是没有必要的,因为这种耦合性是由于对象的创建所带来的,完全可以用创建型模式去解决。在应用时结合创建型设计模式来处理具体的问题。
应用设计模式:
桥接模式(Bridge)来做(多维度变化);
结合上面的例子,增加一个维度"人",
不同的人开着不同的汽车在不同的路上行驶(三个维度);
结合上面增加一个类"人",并重新调用.
代码实现:
客户端调用:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
2
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
4
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
6
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
7
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
8
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
10
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
14
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
15
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
效果及实现要点:
1.Bridge模式使用“对象间的组合关系”解耦了抽象和实现之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化。
2.所谓抽象和实现沿着各自维度的变化,即“子类化”它们,得到各个子类之后,便可以任意它们,从而获得不同路上的不同汽车。
3.Bridge模式有时候类似于多继承方案,但是多继承方案往往违背了类的单一职责原则(即一个类只有一个变化的原因),复用性比较差。Bridge模式是比多继承方案更好的解决方法。
4.Bridge模式的应用一般在“两个非常强的变化维度”,有时候即使有两个变化的维度,但是某个方向的变化维度并不剧烈——换言之两个变化不会导致纵横交错的结果,并不一定要使用Bridge模式。
适用性:
在以下的情况下应当使用桥梁模式:
1.如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的联系。
2.设计要求实现化角色的任何改变不应当影响客户端,或者说实现化角色的改变对客户端是完全透明的。
3.一个构件有多于一个的抽象化角色和实现化角色,系统需要它们之间进行动态耦合。
4.虽然在系统中使用继承是没有问题的,但是由于抽象化角色和具体化角色需要独立变化,设计要求需要独立管理这两者。
总结:
Bridge模式是一个非常有用的模式,也非常复杂,它很好的符合了开放-封闭原则和优先使用对象,而不是继承这两个面向对象原则。
桥接模式与装饰的区别:
装饰模式:
这两个模式在一定程度上都是为了减少子类的数目,避免出现复杂的继承关系。但是它们解决的方法却各有不同,装饰模式把子类中比基类中多出来的部分放到单独的类里面,以适应新功能增加的需要,当我们把描述新功能的类封装到基类的对象里面时,就得到了所需要的子类对象,这些描述新功能的类通过组合可以实现很多的功能组合 .
桥接模式:
桥接模式则把原来的基类的实现化细节抽象出来,在构造到一个实现化的结构中,然后再把原来的基类改造成一个抽象化的等级结构,这样就可以实现系统在多个维度上的独立变化 。
示例源码下载