Bridge桥接(结构型模式)

本文通过坦克实例详细解析桥接模式的原理与应用。探讨如何通过将抽象与实现解耦,达到各自独立变化的目的,适用于拥有两个强变化维度的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动机:
  当一个对象有了两个变化的维度,如何应对这种“多维度的变化”?如何利用面向对象技术来使得这个对象类型可以轻松沿着多个方向变化,而不引入额外的复杂度?

意图:
  将抽象部分与实现部分分离,使它们都可以独立地变化。
  出自:《设计模式》GoF

Bridge模式的几个要点:
  1、Bridge模式使用“对象间的组合关系”解耦了抽象和实现之间固有的绑定关系,使得抽象(Tank的型号)和实现(不现的平台)可以沿着各处的维度来变化。
  2、所谓抽象和实现沿着各处纬度的变化,即“子类化”它们,比如不同的Tank型号子类,和不同的平台子类。得到各个子类之后,便可以任意组它们,从而获得不同平台上的不同型号。
  3、Bridge模式有时候类似于多继承方案,但是多继承方案往往违背单一职责原则(即一个类只有一个变化的原因),复用性比较差。Bridge模式是比多方案更好的解决方法。
  4、Bridge模式的应用一般在“两个非常强的变化维度,有时候即使有两个变化的维度,但是某个方向的变化维度并不剧烈,换言之两个不会导致纵横交错的结果,并不一定要使用Bridge模式。

稳定部分:

 

ContractedBlock.gifExpandedBlockStart.gif
 1None.gifusing System;
 2None.gif
 3None.gif
 4None.gifnamespace Bridge
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// Tank 的摘要说明。
 8ExpandedSubBlockEnd.gif    /// </summary>

 9InBlock.gif    public abstract class Tank
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        protected TankPlatFormImplementation tankImpl;
12InBlock.gif
13InBlock.gif        public Tank(TankPlatFormImplementation tankImpl)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            this.TankImpl = tankImpl;
16ExpandedSubBlockEnd.gif        }

17InBlock.gif
18InBlock.gif        public TankPlatFormImplementation TankImpl
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            get
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                return this.tankImpl;
23ExpandedSubBlockEnd.gif            }

24InBlock.gif            set
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                this.tankImpl = value;
27ExpandedSubBlockEnd.gif            }

28ExpandedSubBlockEnd.gif        }

29InBlock.gif
30InBlock.gif        public abstract void Shot();
31InBlock.gif        public abstract void Run();
32InBlock.gif        public abstract void Stop();
33ExpandedSubBlockEnd.gif    }

34InBlock.gif
35InBlock.gif    public abstract class TankPlatFormImplementation
36ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
37InBlock.gif        public abstract void MoveTankTo(string  to);
38InBlock.gif        public abstract void DrawTank();
39InBlock.gif        public abstract void DoShot();
40ExpandedSubBlockEnd.gif    }

41ExpandedBlockEnd.gif}

42None.gif



变化部分:
  1None.gifusing System;
  2None.gif
  3None.gif
  4None.gifnamespace Bridge
  5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  7InBlock.gif    /// TankA 的摘要说明。
  8ExpandedSubBlockEnd.gif    /// </summary>

  9InBlock.gif    //型号维度的变化
 10InBlock.gif    public class T50 : Tank
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        string to;
 13InBlock.gif        public T50(TankPlatFormImplementation tankImpl) : base(tankImpl)
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 15InBlock.gif
 16ExpandedSubBlockEnd.gif        }

 17InBlock.gif        public override void Shot()
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            //T50
 20InBlock.gif            TankImpl.DrawTank();
 21InBlock.gif            TankImpl.DoShot();
 22InBlock.gif            TankImpl.MoveTankTo(to);
 23ExpandedSubBlockEnd.gif        }

 24InBlock.gif        public override void Run()
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26InBlock.gif            //T50
 27InBlock.gif            TankImpl.DrawTank();
 28InBlock.gif            TankImpl.DoShot();
 29InBlock.gif            TankImpl.MoveTankTo(to);
 30InBlock.gif            Console.Write("T50"+TankImpl.GetType()+"\n");
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif        public override void Stop()
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 34InBlock.gif            //T50
 35InBlock.gif            TankImpl.DrawTank();
 36InBlock.gif            TankImpl.DoShot();
 37InBlock.gif            TankImpl.MoveTankTo(to);
 38ExpandedSubBlockEnd.gif        }

 39ExpandedSubBlockEnd.gif    }

 40InBlock.gif
 41InBlock.gif    public class T75 : Tank
 42ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 43InBlock.gif        string to;
 44InBlock.gif        public T75(TankPlatFormImplementation tankImpl) : base(tankImpl)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            //T75
 47InBlock.gif            TankImpl.DrawTank();
 48InBlock.gif            TankImpl.DoShot();
 49InBlock.gif            TankImpl.MoveTankTo(to);
 50ExpandedSubBlockEnd.gif        }

 51InBlock.gif        public override void Shot()
 52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 53InBlock.gif            //T75
 54InBlock.gif            TankImpl.DrawTank();
 55InBlock.gif            TankImpl.DoShot();
 56InBlock.gif            TankImpl.MoveTankTo(to);
 57ExpandedSubBlockEnd.gif        }

 58InBlock.gif        public override void Run()
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            //T75
 61InBlock.gif            TankImpl.DrawTank();
 62InBlock.gif            TankImpl.DoShot();
 63InBlock.gif            TankImpl.MoveTankTo(to);
 64InBlock.gif            Console.Write("T75"+TankImpl.GetType()+"\n");
 65ExpandedSubBlockEnd.gif        }

 66InBlock.gif        public override void Stop()
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 68InBlock.gif            //T75
 69InBlock.gif            TankImpl.DrawTank();
 70InBlock.gif            TankImpl.DoShot();
 71InBlock.gif            TankImpl.MoveTankTo(to);
 72ExpandedSubBlockEnd.gif        }

 73ExpandedSubBlockEnd.gif    }

 74InBlock.gif
 75InBlock.gif    public class T90 : Tank
 76ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 77InBlock.gif        string to;
 78InBlock.gif        public T90(TankPlatFormImplementation tankImpl) : base(tankImpl)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80InBlock.gif            //T90
 81InBlock.gif            TankImpl.DrawTank();
 82InBlock.gif            TankImpl.DoShot();
 83InBlock.gif            TankImpl.MoveTankTo(to);
 84ExpandedSubBlockEnd.gif        }

 85InBlock.gif        public override void Shot()
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 87InBlock.gif            //T90
 88InBlock.gif            TankImpl.DrawTank();
 89InBlock.gif            TankImpl.DoShot();
 90InBlock.gif            TankImpl.MoveTankTo(to);
 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif        public override void Run()
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 94InBlock.gif            //T90
 95InBlock.gif            TankImpl.DrawTank();
 96InBlock.gif            TankImpl.DoShot();
 97InBlock.gif            TankImpl.MoveTankTo(to);
 98InBlock.gif            Console.Write("T90"+TankImpl.GetType()+"\n");
 99ExpandedSubBlockEnd.gif        }

100InBlock.gif        public override void Stop()
101ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
102InBlock.gif            //T90
103InBlock.gif            TankImpl.DrawTank();
104InBlock.gif            TankImpl.DoShot();
105InBlock.gif            TankImpl.MoveTankTo(to);
106ExpandedSubBlockEnd.gif        }

107ExpandedSubBlockEnd.gif    }

108InBlock.gif
109InBlock.gif    //平台维度的变化
110InBlock.gif    public class PCTankImplementation : TankPlatFormImplementation
111ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
112InBlock.gif        public override void MoveTankTo(string  to)
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114InBlock.gif
115ExpandedSubBlockEnd.gif        }

116InBlock.gif        public override void DrawTank()
117ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
118InBlock.gif            Console.Write("PCTankImplementation \n");
119InBlock.gif
120ExpandedSubBlockEnd.gif        }

121InBlock.gif        public override void DoShot()
122ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
123InBlock.gif
124ExpandedSubBlockEnd.gif        }

125ExpandedSubBlockEnd.gif    }

126InBlock.gif
127InBlock.gif    public class MobileTankImplementation : TankPlatFormImplementation
128ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
129InBlock.gif        public override void MoveTankTo(string to)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131InBlock.gif
132ExpandedSubBlockEnd.gif        }

133InBlock.gif        public override void DrawTank()
134ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
135InBlock.gif            
136InBlock.gif            Console.Write("MobileTankImplementation \n");
137InBlock.gif
138ExpandedSubBlockEnd.gif        }

139InBlock.gif        public override void DoShot()
140ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
141InBlock.gif
142ExpandedSubBlockEnd.gif        }

143ExpandedSubBlockEnd.gif    }

144InBlock.gif
145ExpandedBlockEnd.gif}

146None.gif


主程序:
 1None.gifusing System;
 2None.gif
 3None.gif
 4None.gifnamespace Bridge
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// Class1 的摘要说明。
 8ExpandedSubBlockEnd.gif    /// </summary>

 9InBlock.gif    class Class1
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
12InBlock.gif        /// 应用程序的主入口点。
13ExpandedSubBlockEnd.gif        /// </summary>

14InBlock.gif        [STAThread]
15InBlock.gif        static void Main(string[] args)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            //用于多个维度变化的对象
18InBlock.gif             
19InBlock.gif            //变化点
20InBlock.gif            //Mobile T50 的组合
21InBlock.gif            TankPlatFormImplementation TankImpl = new MobileTankImplementation();
22InBlock.gif            T50 tank = new T50(TankImpl);
23InBlock.gif            tank.Run();
24InBlock.gif            //PC T90 的组合
25InBlock.gif            TankPlatFormImplementation TankImp = new PCTankImplementation();
26InBlock.gif            T90 tank1 = new T90(TankImp);
27InBlock.gif            tank1.Run();
28InBlock.gif            Console.ReadLine();
29ExpandedSubBlockEnd.gif        }

30ExpandedSubBlockEnd.gif    }

31ExpandedBlockEnd.gif}

32None.gif 
33None.gif

转载于:https://www.cnblogs.com/walker/archive/2006/08/17/479610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值