设计模式之桥接模式(Bridge)

1.引言
一位哲学家说“你永远也看不到一条一模一样的河”,不考虑哲学上的对错,光是从变化的角度看,的确是这样,因为尽管一条小河,在空间上没有发生变化,但在时间坐标上已经发生了改变,世上万物都在做着这样的变化,但是你把一块石头放在你的鱼缸里面,几天后,你会发现它还是那块石头,这说明事物尽管都有向多个维度变化的特性,但是有些事物是相对比较稳定的,而有些事物多维度变化确实比较激励的。我们用继承(inherit)解决了变化不稳定的事物的扩展问题, 如何利用面向对象的技术来使得事物能够轻松的沿着多个方向进行变化,实现起来又不是很复杂呢?这就要使用 Bridge 模式。
2)意图
使抽象部分和实现部分分离,使他们都能够独立的变化(GOF)
采用继承的情况下,具体实现依赖于抽象定义,抽象定义被看作是相对稳定的。而对于像多个维度变化激烈的对象来说 ,抽象定义也是不稳定的。这时候,如果想尽可能付出小的代价,获得最大的扩展,采用桥接模式是一个好的主意
3)结构图

4)以坦克大战中的坦克为例,说明桥接模式的优点
实例代码为:
ContractedBlock.gif ExpandedBlockStart.gif Abstraction
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace BridgeStudy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 8InBlock.gif    /// 坦克基类
 9ExpandedSubBlockEnd.gif    /// </summary>

10InBlock.gif    public abstract class TankBase
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        TankBase1 tank = null;
13InBlock.gif        public TankBase1 Tank
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            get
16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
17InBlock.gif                return tank;
18ExpandedSubBlockEnd.gif            }

19InBlock.gif            set
20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
21InBlock.gif                tank = value;
22ExpandedSubBlockEnd.gif            }

23ExpandedSubBlockEnd.gif        }

24InBlock.gif
25InBlock.gif        public TankBase1 TankBase1
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            get
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                throw new System.NotImplementedException();
30ExpandedSubBlockEnd.gif            }

31InBlock.gif            set
32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
33ExpandedSubBlockEnd.gif            }

34ExpandedSubBlockEnd.gif        }

35InBlock.gif           
36InBlock.gif        public virtual void Shoot()
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            tank.Shoot();
39ExpandedSubBlockEnd.gif        }

40ExpandedSubBlockEnd.gif    }

41ExpandedBlockEnd.gif}

42None.gif

ContractedBlock.gif ExpandedBlockStart.gif Implentor
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace BridgeStudy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 8InBlock.gif    /// 射击炮弹种类不同的坦克基类
 9ExpandedSubBlockEnd.gif    /// </summary>

10InBlock.gif    public abstract class TankBase1
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif            
13InBlock.gif        public abstract void Shoot();
14ExpandedSubBlockEnd.gif    }

15ExpandedBlockEnd.gif}

16None.gif

ContractedBlock.gif ExpandedBlockStart.gif RefinedAbstractuib
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace BridgeStudy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 8InBlock.gif    /// 纵向射击坦克的实现类
 9ExpandedSubBlockEnd.gif    /// </summary>

10InBlock.gif    public class VerticalTank:TankBase
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{       
12InBlock.gif        public override void Shoot()
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14InBlock.gif            base.Shoot();
15InBlock.gif            Console.WriteLine("shoot vertical dot.gif");
16ExpandedSubBlockEnd.gif        }

17ExpandedSubBlockEnd.gif    }

18ExpandedBlockEnd.gif}

19None.gif
20None.gifusing System;
21None.gifusing System.Collections.Generic;
22None.gifusing System.Text;
23None.gif
24None.gifnamespace BridgeStudy
25ExpandedBlockStart.gifContractedBlock.gifdot.gif{
26ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
27InBlock.gif    /// 横向设计坦克的实现类
28ExpandedSubBlockEnd.gif    /// </summary>

29InBlock.gif    public class HorizontalTank : TankBase
30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{       
31InBlock.gif        public override void Shoot()
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif            base.Shoot();
34InBlock.gif            Console.WriteLine("shoot horizontal dot.gif");
35ExpandedSubBlockEnd.gif        }

36ExpandedSubBlockEnd.gif    }

37ExpandedBlockEnd.gif}

38None.gif
ContractedBlock.gif ExpandedBlockStart.gif ConcreteImplementor
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace BridgeStudy
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 8InBlock.gif    /// 普通炮弹坦克实现类
 9ExpandedSubBlockEnd.gif    /// </summary>

10InBlock.gif    public class CommonTank : TankBase1
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        public override void Shoot()
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14InBlock.gif            Console.WriteLine("shoot common cannonballdot.gif");
15ExpandedSubBlockEnd.gif        }

16ExpandedSubBlockEnd.gif    }

17ExpandedBlockEnd.gif}

18None.gif
19None.gif
20None.gifusing System;
21None.gifusing System.Collections.Generic;
22None.gifusing System.Text;
23None.gif
24None.gifnamespace BridgeStudy
25ExpandedBlockStart.gifContractedBlock.gifdot.gif{
26ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
27InBlock.gif    /// 激光坦克实现类
28ExpandedSubBlockEnd.gif    /// </summary>

29InBlock.gif    public class LaserTank:TankBase1
30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
31InBlock.gif        public override void Shoot()
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif            Console.WriteLine("shoot common Laserdot.gif");
34ExpandedSubBlockEnd.gif        }

35ExpandedSubBlockEnd.gif    }

36ExpandedBlockEnd.gif}

37None.gif
类关系图为

运行结果:
源代码: /Files/jillzhang/BridgeStudy.rar
总结:
1。Bri dge 模式使用“对象间的组合关系”解耦了抽象和实现之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化。

2.所谓抽象和实现沿着各自维度的变化,即“子类化”它们,得到各个子类之后,便可以任意它们,从而获得不同平台上的不同型号。

3.Bridge模式有时候类似于多继承方案,但是多继承方案往往违背了类的单一职责原则(即一个类只有一个变化的原因),复用性比较差。Bridge模式是比多继承方案更好的解决方法。

4.Bridge模式的应用一般在“两个非常强的变化维度”,有时候即使有两个变化的维度,但是某个方向的变化维度并不剧烈——换言之两个变化不会导致纵横交错的结果,并不一定要使用Bridge模式。

适用性

在以下的情况下应当使用桥梁模式:

1.如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的联系。

2.设计要求实现化角色的任何改变不应当影响客户端,或者说实现化角色的改变对客户端是完全透明的。

3.一个构件有多于一个的抽象化角色和实现化角色,系统需要它们之间进行动态耦合。

4.虽然在系统中使用继承是没有问题的,但是由于抽象化角色和具体化角色需要独立变化,设计要求需要独立管理这两者。

Bridge模式是一个非常有用的模式,也非常复杂,它很好的符合了开放-封闭原则和优先使用对象,而不是继承这两个面向对象原则。
关于桥接模式更好,更详细的介绍请访问

转载于:https://www.cnblogs.com/erichzhou/archive/2007/03/29/693065.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值