转:设计模式-----桥接模式(Bridge Pattern)

时间:2023-03-08 16:55:49
转:设计模式-----桥接模式(Bridge Pattern)

转自:http://www.cnblogs.com/houleixx/archive/2008/02/23/1078877.html

记得看原始链接的评论。

学习设计模式也有一段时间了,今天就把我整理的一篇课程和大家分享,有不妥之处欢迎指出. 
生活中的一个例子:
    就拿汽车在路上行驶的来说。即有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶。这你会发现,对于交通工具(汽车)有不同的类型,然而它们所行驶的环境(路)也在变化,在软件系统中就要适应两个方面的变化?怎样实现才能应对这种变化呢?
概述:
在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就要使用Bridge模式。
意图:
   将抽象部分与实现部分分离,使它们都可以独立的变化。
                                                                    ——《设计模式》GOF 
结构图:
                                 转:设计模式-----桥接模式(Bridge Pattern)
传统的做法:
        通过类继承的方式来做上面的例子;
先看一下类结构图:
      转:设计模式-----桥接模式(Bridge Pattern)
代码实现:
 1转:设计模式-----桥接模式(Bridge Pattern)namespace CarRunOnRoad
 2转:设计模式-----桥接模式(Bridge Pattern){
 3转:设计模式-----桥接模式(Bridge Pattern)    //路的基类;
 4转:设计模式-----桥接模式(Bridge Pattern)    public  class Road
 5转:设计模式-----桥接模式(Bridge Pattern)    {
 6转:设计模式-----桥接模式(Bridge Pattern)        public virtual void Run()
 7转:设计模式-----桥接模式(Bridge Pattern)        {
 8转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("在路上");
 9转:设计模式-----桥接模式(Bridge Pattern)        }
10转:设计模式-----桥接模式(Bridge Pattern)    }
11转:设计模式-----桥接模式(Bridge Pattern)    //高速公路;
12转:设计模式-----桥接模式(Bridge Pattern)    public class SpeedWay : Road
13转:设计模式-----桥接模式(Bridge Pattern)    {
14转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
15转:设计模式-----桥接模式(Bridge Pattern)        {
16转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("高速公路");
17转:设计模式-----桥接模式(Bridge Pattern)        }
18转:设计模式-----桥接模式(Bridge Pattern)    }
19转:设计模式-----桥接模式(Bridge Pattern)    //市区街道;
20转:设计模式-----桥接模式(Bridge Pattern)    public class Street : Road
21转:设计模式-----桥接模式(Bridge Pattern)    {
22转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
23转:设计模式-----桥接模式(Bridge Pattern)        {
24转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("市区街道");
25转:设计模式-----桥接模式(Bridge Pattern)        }
26转:设计模式-----桥接模式(Bridge Pattern)    }
27转:设计模式-----桥接模式(Bridge Pattern)    //小汽车在高速公路上行驶;
28转:设计模式-----桥接模式(Bridge Pattern)    public class CarOnSpeedWay : SpeedWay
29转:设计模式-----桥接模式(Bridge Pattern)    {
30转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
31转:设计模式-----桥接模式(Bridge Pattern)        {
32转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("小汽车在高速公路上行驶");
33转:设计模式-----桥接模式(Bridge Pattern)        }
34转:设计模式-----桥接模式(Bridge Pattern)    }
35转:设计模式-----桥接模式(Bridge Pattern)    //公共汽车在高速公路上行驶;
36转:设计模式-----桥接模式(Bridge Pattern)    public class BusOnSpeedWay : SpeedWay
37转:设计模式-----桥接模式(Bridge Pattern)    {
38转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
39转:设计模式-----桥接模式(Bridge Pattern)        {
40转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("公共汽车在高速公路上行驶");
41转:设计模式-----桥接模式(Bridge Pattern)        }
42转:设计模式-----桥接模式(Bridge Pattern)    }
43转:设计模式-----桥接模式(Bridge Pattern)    //小汽车在市区街道上行驶;
44转:设计模式-----桥接模式(Bridge Pattern)    public class CarOnStreet : Street
45转:设计模式-----桥接模式(Bridge Pattern)    {
46转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
47转:设计模式-----桥接模式(Bridge Pattern)        {
48转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("汽车在街道上行驶");
49转:设计模式-----桥接模式(Bridge Pattern)        }
50转:设计模式-----桥接模式(Bridge Pattern)    }
51转:设计模式-----桥接模式(Bridge Pattern)    //公共汽车在市区街道上行驶;
52转:设计模式-----桥接模式(Bridge Pattern)    public class BusOnStreet : Street
53转:设计模式-----桥接模式(Bridge Pattern)    {
54转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
55转:设计模式-----桥接模式(Bridge Pattern)        {
56转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("公共汽车在街道上行驶");
57转:设计模式-----桥接模式(Bridge Pattern)        }
58转:设计模式-----桥接模式(Bridge Pattern)    }
59转:设计模式-----桥接模式(Bridge Pattern)   
60转:设计模式-----桥接模式(Bridge Pattern)}

客户端调用:

 1转:设计模式-----桥接模式(Bridge Pattern)static void Main(string[] args)
 2转:设计模式-----桥接模式(Bridge Pattern)        {
 3转:设计模式-----桥接模式(Bridge Pattern)            //小汽车在高速公路上行驶
 4转:设计模式-----桥接模式(Bridge Pattern)            CarOnSpeedWay Car = new CarOnSpeedWay();
 5转:设计模式-----桥接模式(Bridge Pattern)            Car.Run();
 6转:设计模式-----桥接模式(Bridge Pattern)
 7转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("===========================");
 8转:设计模式-----桥接模式(Bridge Pattern)
 9转:设计模式-----桥接模式(Bridge Pattern)            //公共汽车在街道上行驶
10转:设计模式-----桥接模式(Bridge Pattern)            BusOnStreet Bus = new BusOnStreet();
11转:设计模式-----桥接模式(Bridge Pattern)            Bus.Run();
12转:设计模式-----桥接模式(Bridge Pattern)
13转:设计模式-----桥接模式(Bridge Pattern)            Console.Read();
14转:设计模式-----桥接模式(Bridge Pattern)        }

缺点:
     但是我们说这样的设计是脆弱的,仔细分析就可以发现,它还是存在很多问题,首先它在遵循开放-封闭原则的同时,违背了类的单一职责原则,即一个类只有一个引起它变化的原因,而这里引起变化的原因却有两个,即路类型的变化和汽车类型的变化;其次是重复代码会很多,不同的汽车在不同的路上行驶也会有一部分的代码是相同的;再次是类的结构过于复杂,继承关系太多,难于维护,最后最致命的一点是扩展性太差。如果变化沿着汽车的类型和不同的道路两个方向变化,我们会看到这个类的结构会迅速的变庞大。

应用设计模式
       桥接模式(Bridge)来做;
先看一下类结构图:
                      转:设计模式-----桥接模式(Bridge Pattern)
                           转:设计模式-----桥接模式(Bridge Pattern)
代码实现:

 1转:设计模式-----桥接模式(Bridge Pattern)namespace CarRunOnRoad_Bridge_
 2转:设计模式-----桥接模式(Bridge Pattern){
 3转:设计模式-----桥接模式(Bridge Pattern)
 4转:设计模式-----桥接模式(Bridge Pattern)    //抽象路
 5转:设计模式-----桥接模式(Bridge Pattern)    public abstract class AbstractRoad
 6转:设计模式-----桥接模式(Bridge Pattern)    {
 7转:设计模式-----桥接模式(Bridge Pattern)        protected AbstractCar car;
 8转:设计模式-----桥接模式(Bridge Pattern)        public AbstractCar Car
 9转:设计模式-----桥接模式(Bridge Pattern)        {
10转:设计模式-----桥接模式(Bridge Pattern)            set
11转:设计模式-----桥接模式(Bridge Pattern)            {
12转:设计模式-----桥接模式(Bridge Pattern)                car = value;
13转:设计模式-----桥接模式(Bridge Pattern)            }
14转:设计模式-----桥接模式(Bridge Pattern)        }
15转:设计模式-----桥接模式(Bridge Pattern)
16转:设计模式-----桥接模式(Bridge Pattern)        public abstract void Run();
17转:设计模式-----桥接模式(Bridge Pattern)    }
18转:设计模式-----桥接模式(Bridge Pattern)
19转:设计模式-----桥接模式(Bridge Pattern)    //高速公路
20转:设计模式-----桥接模式(Bridge Pattern)    public class SpeedWay : AbstractRoad
21转:设计模式-----桥接模式(Bridge Pattern)    {
22转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
23转:设计模式-----桥接模式(Bridge Pattern)        {
24转:设计模式-----桥接模式(Bridge Pattern)            car.Run();
25转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("高速公路上行驶");
26转:设计模式-----桥接模式(Bridge Pattern)        }
27转:设计模式-----桥接模式(Bridge Pattern)    }
28转:设计模式-----桥接模式(Bridge Pattern)
29转:设计模式-----桥接模式(Bridge Pattern)    //市区街道
30转:设计模式-----桥接模式(Bridge Pattern)    public class Street : AbstractRoad
31转:设计模式-----桥接模式(Bridge Pattern)    {
32转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
33转:设计模式-----桥接模式(Bridge Pattern)        {
34转:设计模式-----桥接模式(Bridge Pattern)            car.Run();
35转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("市区街道上行驶");
36转:设计模式-----桥接模式(Bridge Pattern)        }
37转:设计模式-----桥接模式(Bridge Pattern)    }
38转:设计模式-----桥接模式(Bridge Pattern)}
 1转:设计模式-----桥接模式(Bridge Pattern)namespace CarRunOnRoad_Bridge_
 2转:设计模式-----桥接模式(Bridge Pattern){
 3转:设计模式-----桥接模式(Bridge Pattern)    //抽象汽车 
 4转:设计模式-----桥接模式(Bridge Pattern)    public abstract class AbstractCar
 5转:设计模式-----桥接模式(Bridge Pattern)    {
 6转:设计模式-----桥接模式(Bridge Pattern)        public abstract void Run();
 7转:设计模式-----桥接模式(Bridge Pattern)    }
 8转:设计模式-----桥接模式(Bridge Pattern)
 9转:设计模式-----桥接模式(Bridge Pattern)    //小汽车;
10转:设计模式-----桥接模式(Bridge Pattern)    public class Car : AbstractCar
11转:设计模式-----桥接模式(Bridge Pattern)    {
12转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
13转:设计模式-----桥接模式(Bridge Pattern)        {
14转:设计模式-----桥接模式(Bridge Pattern)            Console.Write("小汽车在");
15转:设计模式-----桥接模式(Bridge Pattern)        }
16转:设计模式-----桥接模式(Bridge Pattern)    }
17转:设计模式-----桥接模式(Bridge Pattern)
18转:设计模式-----桥接模式(Bridge Pattern)    //公共汽车
19转:设计模式-----桥接模式(Bridge Pattern)    public class Bus : AbstractCar
20转:设计模式-----桥接模式(Bridge Pattern)    {
21转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
22转:设计模式-----桥接模式(Bridge Pattern)        {
23转:设计模式-----桥接模式(Bridge Pattern)            Console.Write("公共汽车在");
24转:设计模式-----桥接模式(Bridge Pattern)        }
25转:设计模式-----桥接模式(Bridge Pattern)    }
26转:设计模式-----桥接模式(Bridge Pattern)}

客户端调用:

 1转:设计模式-----桥接模式(Bridge Pattern) static void Main(string[] args)
 2转:设计模式-----桥接模式(Bridge Pattern)        {
 3转:设计模式-----桥接模式(Bridge Pattern)            //小汽车在高速公路上行驶;
 4转:设计模式-----桥接模式(Bridge Pattern)            AbstractRoad Road1 = new SpeedWay();
 5转:设计模式-----桥接模式(Bridge Pattern)            Road1.Car = new Car();
 6转:设计模式-----桥接模式(Bridge Pattern)            Road1.Run();
 7转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("=========================");
 8转:设计模式-----桥接模式(Bridge Pattern)
 9转:设计模式-----桥接模式(Bridge Pattern)            //公共汽车在高速公路上行驶;
10转:设计模式-----桥接模式(Bridge Pattern)            AbstractRoad Road2 = new SpeedWay();
11转:设计模式-----桥接模式(Bridge Pattern)            Road2.Car = new Bus();
12转:设计模式-----桥接模式(Bridge Pattern)            Road2.Run();
13转:设计模式-----桥接模式(Bridge Pattern)
14转:设计模式-----桥接模式(Bridge Pattern)           
15转:设计模式-----桥接模式(Bridge Pattern)
16转:设计模式-----桥接模式(Bridge Pattern)            Console.Read();
17转:设计模式-----桥接模式(Bridge Pattern)        }

可以看到,通过对象组合的方式,Bridge 模式把两个角色之间的继承关系改为了耦合的关系,从而使这两者可以从容自若的各自独立的变化,这也是Bridge模式的本意。
      这样增加了客户程序与路与汽车的耦合。其实这样的担心是没有必要的,因为这种耦合性是由于对象的创建所带来的,完全可以用创建型模式去解决。在应用时结合创建型设计模式来处理具体的问题。
应用设计模式:
       桥接模式(Bridge)来做(多维度变化);
       结合上面的例子,增加一个维度"人",不同的人开着不同的汽车在不同的路上行驶(三个维度);
       结合上面增加一个类"人",并重新调用.
代码实现:

 1转:设计模式-----桥接模式(Bridge Pattern)namespace CarRunOnRoad_Bridge_
 2转:设计模式-----桥接模式(Bridge Pattern){
 3转:设计模式-----桥接模式(Bridge Pattern)    abstract class people
 4转:设计模式-----桥接模式(Bridge Pattern)    {
 5转:设计模式-----桥接模式(Bridge Pattern)        AbstractRoad road;
 6转:设计模式-----桥接模式(Bridge Pattern)        public AbstractRoad Road
 7转:设计模式-----桥接模式(Bridge Pattern)        {
 8转:设计模式-----桥接模式(Bridge Pattern)            get
 9转:设计模式-----桥接模式(Bridge Pattern)            {
10转:设计模式-----桥接模式(Bridge Pattern)                return road;
11转:设计模式-----桥接模式(Bridge Pattern)            }
12转:设计模式-----桥接模式(Bridge Pattern)            set
13转:设计模式-----桥接模式(Bridge Pattern)            {
14转:设计模式-----桥接模式(Bridge Pattern)                road = value;
15转:设计模式-----桥接模式(Bridge Pattern)            }
16转:设计模式-----桥接模式(Bridge Pattern)        }
17转:设计模式-----桥接模式(Bridge Pattern)        public abstract void Run();
18转:设计模式-----桥接模式(Bridge Pattern)
19转:设计模式-----桥接模式(Bridge Pattern)    }
20转:设计模式-----桥接模式(Bridge Pattern)    class Man : people
21转:设计模式-----桥接模式(Bridge Pattern)    {
22转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
23转:设计模式-----桥接模式(Bridge Pattern)        {
24转:设计模式-----桥接模式(Bridge Pattern)            Console.Write("男人开着");
25转:设计模式-----桥接模式(Bridge Pattern)            Road.Run();
26转:设计模式-----桥接模式(Bridge Pattern)        }
27转:设计模式-----桥接模式(Bridge Pattern)    }
28转:设计模式-----桥接模式(Bridge Pattern)
29转:设计模式-----桥接模式(Bridge Pattern)    class WoMan : people
30转:设计模式-----桥接模式(Bridge Pattern)    {
31转:设计模式-----桥接模式(Bridge Pattern)        public override void Run()
32转:设计模式-----桥接模式(Bridge Pattern)        {
33转:设计模式-----桥接模式(Bridge Pattern)            Console.Write("女人开着");
34转:设计模式-----桥接模式(Bridge Pattern)            Road.Run();
35转:设计模式-----桥接模式(Bridge Pattern)        }
36转:设计模式-----桥接模式(Bridge Pattern)    }
37转:设计模式-----桥接模式(Bridge Pattern)}

客户端调用:

 1转:设计模式-----桥接模式(Bridge Pattern) static void Main(string[] args)
 2转:设计模式-----桥接模式(Bridge Pattern)        {
 3转:设计模式-----桥接模式(Bridge Pattern)            
 4转:设计模式-----桥接模式(Bridge Pattern)            //男人开着公共汽车在高速公路上行驶;
 5转:设计模式-----桥接模式(Bridge Pattern)            Console.WriteLine("=========================");
 6转:设计模式-----桥接模式(Bridge Pattern)
 7转:设计模式-----桥接模式(Bridge Pattern)            AbstractRoad Road3 = new SpeedWay();
 8转:设计模式-----桥接模式(Bridge Pattern)            Road3.Car = new Bus();
 9转:设计模式-----桥接模式(Bridge Pattern)
10转:设计模式-----桥接模式(Bridge Pattern)            people p = new Man();
11转:设计模式-----桥接模式(Bridge Pattern)            p.Road = Road3;
12转:设计模式-----桥接模式(Bridge Pattern)            p.Run();
13转:设计模式-----桥接模式(Bridge Pattern)
14转:设计模式-----桥接模式(Bridge Pattern)            Console.Read();
15转:设计模式-----桥接模式(Bridge Pattern)        }

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

适用性:
   在以下的情况下应当使用桥梁模式:
1.如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的联系。 
2.设计要求实现化角色的任何改变不应当影响客户端,或者说实现化角色的改变对客户端是完全透明的。
3.一个构件有多于一个的抽象化角色和实现化角色,系统需要它们之间进行动态耦合。 
4.虽然在系统中使用继承是没有问题的,但是由于抽象化角色和具体化角色需要独立变化,设计要求需要独立管理这两者。
总结:
      Bridge模式是一个非常有用的模式,也非常复杂,它很好的符合了开放-封闭原则和优先使用对象,而不是继承这两个面向对象原则。

桥接模式与装饰的区别:
装饰模式:

      这两个模式在一定程度上都是为了减少子类的数目,避免出现复杂的继承关系。但是它们解决的方法却各有不同,装饰模式把子类中比基类中多出来的部分放到单独的类里面,以适应新功能增加的需要,当我们把描述新功能的类封装到基类的对象里面时,就得到了所需要的子类对象,这些描述新功能的类通过组合可以实现很多的功能组合 .
桥接模式:
          桥接模式则把原来的基类的实现化细节抽象出来,在构造到一个实现化的结构中,然后再把原来的基类改造成一个抽象化的等级结构,这样就可以实现系统在多个维度上的独立变化 。

示例源码下载

(作者:侯垒