桥接模式 bridge
桥接模式: 将抽象和行为划分开来,各自独立,但能动态的结合。
在这里抽象不是表示抽象类或接口,而是表示对应于现实的一种实体的集合。比如说人就是一种抽象,吃饭就是一种行为。抽象不一定有行为。但行为一定是依附于抽象的。对于抽象的不同子集会有不同的行为。而同样的行为也可能属于抽象的不同子集。这样我们可以把行为抽取出来进行不重不漏的分类封装。从而和抽象的子集进行"排列组合"。
类图:
下面来谈一谈防盗门。如果我们把门设计成和门的防盗功能结合在一起。
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--> 1
public abstract class Door
2
{
3
public void Open()
4
{
5
System.Diagnostics.Debug.WriteLine("base open;");
6
}
7
public void Close()
8
{
9
System.Diagnostics.Debug.WriteLine("base close;");
10
}
11
public virtual void AgainstTheft()
12
{
13
System.Diagnostics.Debug.WriteLine("base AgainstTheft;");
14
}
15
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1
class WoodAgStrikeDoor:Door
2
{
3
public override void AgainstTheft()
4
{
5
System.Diagnostics.Debug.WriteLine("AgStrike AgainstStrike;");
6
//这个用来方砸的
7
}
8
}
2
3
4
5
6
7
8
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--> 1
public class WoodAgPrizeDoor:Door
2
{
3
public void Open()
4
{
5
System.Diagnostics.Debug.WriteLine("AgThDoor open;");
6
}
7
public void Close()
8
{
9
System.Diagnostics.Debug.WriteLine("AgThDoor close;");
10
}
11
public override void AgainstTheft()
12
{
13
System.Diagnostics.Debug.WriteLine("AgThDoor AgainstTheft;");
14
//这个用来防撬
15
}
16
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
这样好像是没什么问题吧。不过贼就只会撬,砸什么的吗?他还会砍什么的。那有多少种我们就实现多少种AgainstTheft()吧。这个不存在什么问题。
天啊,刚才讨论的仅仅是木门,别忘了世界上可不止一种门。什么木门啊,铁门啊,什么塑料门啊,钢门就别算了。那这么一算是三种门三种防盗方式。 3*3就要实现9个类。估计这时候你要砸键盘了。要是30*30呢人就疯掉了。
怎么去改进呢,仔细思考一下,我们的Door既有门的功能又有防盗的功能。但是现实还可以是这么一种情况,门只具有门的功能加上防盗器才具有防盗的功能。门和防盗器之间可以自由的组合,那现在好了 30*30 变成 30+30啦。这个世界清净了,不用做这么多傻逼的重复劳动了。
那重新设计一下吧:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--> 1
public abstract class Door
2
{
3
protected ITheftAgainstor theftAgainstor;
4
5
public void SetTheftAgainstor(ITheftAgainstor obj)
6
{
7
theftAgainstor = obj;
8
}
9
public void Open()
10
{
11
System.Diagnostics.Debug.WriteLine("base open;");
12
}
13
public void Close()
14
{
15
System.Diagnostics.Debug.WriteLine("base close;");
16
}
17
public void AgainstTheft()
18
{
19
theftAgainstor.AgainstTheft();
20
}
21
22
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1
public class WoodDoor : Door
2
{
3
public void AgainstTheft()
4
{
5
6
}
7
}
2
3
4
5
6
7
//防盗器的接口
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1
public interface ITheftAgainstor
2
{
3
void AgainstTheft();
4
}
2
3
4
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1
public class StrikeAgainstor:Strategy.ITheftAgainstor
2
{
3
public void AgainstTheft()
4
{
5
System.Diagnostics.Debug.WriteLine(this.ToString());
6
}
7
}
2
3
4
5
6
7
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1
public class ChopAgainstor : ITheftAgainstor
2
{
3
public void AgainstTheft()
4
{
5
System.Diagnostics.Debug.WriteLine(this.ToString());
6
}
7
}
2
3
4
5
6
7
调用代码
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1
Door door = new Strategy.WoodDoor();//造一张木门了。
2
door.SetTheftAgainstor(new StrikeAgainstor());//防砸的
3
door.AgainstTheft();
4
5
door.SetTheftAgainstor(new ChopAgainstor());//防砍的
6
door.AgainstTheft();
接下来你可以试试 铁门啊什么的。
2
3
4
5
6
接下来你可以试试 铁门啊什么的。
你可能在想,防盗器本身不也是一种现实中实体的集合吗,不也是一种抽象吗。不过自然、人类就是天生的解耦高手,谁知道世界上的第一张防盗门是不是门本身具有防盗功能呢(这个没必要考究了,我本身也怀疑这样的想法,不做个假设又何妨呢),只是在时间和人类的智慧在悄悄的改变这个世界,重构这个世界。防盗的功能也被人类智慧封装到一个叫防盗器的类里面了。程序本身也是对这个世界的模型的一种模拟。如果你觉得下面这个设计本该如此,那么它就是对这个世界的合理的模拟了。