23种设计模式--Bridge模式
Posted 灬鬼谷灬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了23种设计模式--Bridge模式相关的知识,希望对你有一定的参考价值。
面向对象的设计原则:高内聚、低耦合
软件重构原则:小步快跑------抽取的思想(抽取函数、抽取类、抽取接口);对扩展开放、对修改封闭
设计模式分类如下:
下面这篇文章对Bridge模式讲解的通俗易懂,于是转了过来。
就拿汽车在路上行驶的来说。即有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶。这你会发现,对于交通工具(汽车)有不同的类型,然而它们所行驶的环境(路)也在变化,在软件系统中就要适应两个方面的变化?怎样实现才能应对这种变化呢?
概述:
在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就要使用Bridge模式。
意图:
将抽象部分与实现部分分离,使它们都可以独立的变化。
——《设计模式》GOF
结构图:
![](https://image.cha138.com/20210501/4f026034e25d429c9590befcd5f1f587.jpg)
传统的做法:
通过类继承的方式来做上面的例子;
先看一下类结构图:
![](https://image.cha138.com/20210501/185852c504c844eda6c69a1ebe4666a6.jpg)
代码实现:
1
namespace
CarRunOnRoad
2
{
3
//路的基类;
4
public class Road
5
{
6
public virtual void Run()
7
{
8
Console.WriteLine("在路上");
9
}
10
}
11
//高速公路;
12
public class SpeedWay : Road
13
{
14
public override void Run()
15
{
16
Console.WriteLine("高速公路");
17
}
18
}
19
//市区街道;
20
public class Street : Road
21
{
22
public override void Run()
23
{
24
Console.WriteLine("市区街道");
25
}
26
}
27
//小汽车在高速公路上行驶;
28
public class CarOnSpeedWay : SpeedWay
29
{
30
public override void Run()
31
{
32
Console.WriteLine("小汽车在高速公路上行驶");
33
}
34
}
35
//公共汽车在高速公路上行驶;
36
public class BusOnSpeedWay : SpeedWay
37
{
38
public override void Run()
39
{
40
Console.WriteLine("公共汽车在高速公路上行驶");
41
}
42
}
43
//小汽车在市区街道上行驶;
44
public class CarOnStreet : Street
45
{
46
public override void Run()
47
{
48
Console.WriteLine("汽车在街道上行驶");
49
}
50
}
51
//公共汽车在市区街道上行驶;
52
public class BusOnStreet : Street
53
{
54
public override void Run()
55
{
56
Console.WriteLine("公共汽车在街道上行驶");
57
}
58
}
59
60
}
![](https://image.cha138.com/20210501/fc9eaf225e104065b69eec2a894d97e0.jpg)
2
![](https://image.cha138.com/20210501/f6052862c863462d8ffe6c232fad857d.jpg)
3
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
4
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
5
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
6
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
7
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
8
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
9
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
10
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
11
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
12
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
13
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
14
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
15
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
16
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
17
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
18
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
19
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
20
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
21
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
22
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
23
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
24
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
25
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
26
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
27
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
28
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
29
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
30
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
31
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
32
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
33
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
34
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
35
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
36
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
37
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
38
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
39
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
40
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
41
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
42
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
43
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
44
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
45
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
46
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
47
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
48
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
49
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
50
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
51
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
52
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
53
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
54
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
55
![](https://image.cha138.com/20210501/b72510a2f23b46a5bce53cd826b6ea82.jpg)
56
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
57
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
58
![](https://image.cha138.com/20210501/e09002295fc64a12bff7c7c275172ed5.jpg)
59
![](https://image.cha138.com/20210501/bfc679d4eefc42d2921b00785fbed952.jpg)
60
![](https://image.cha138.com/20210501/d46dcebf81044bd4ba24c8f9999e629e.jpg)
以上是关于23种设计模式--Bridge模式的主要内容,如果未能解决你的问题,请参考以下文章