桥接模式

桥接模式定义

桥接模式(Bridge),将抽象部分与它的实现部分分离,使它们都可以独立地变化。注意:抽象与实现的分离并不是抽象类与其派生类分离,实现指的是抽象类和它的派生类用来实现自己的对象。这里涉及到合成/聚合原则。

桥接模式结构图

桥接模式结构图如下所示:

图 01 桥接模式结构图

桥接模式套用代码

 1 #include "iostream"
 2 using namespace std;
 3 
 4 class Implementor
 5 {
 6 public:
 7     virtual void Operation() = 0;
 8     virtual ~Implementor()
 9     {
10     
11     }
12 };
13 
14 class ConcreteImplementorA : public Implementor
15 {
16 public:
17     virtual void Operation()
18     {
19         cout << "具体实现A的方法执行" << endl;
20     }
21 
22     virtual ~ConcreteImplementorA()
23     {
24     
25     }
26 };
27 
28 class ConcreteImplementorB : public Implementor
29 {
30 public:
31     virtual void Operation()
32     {
33         cout << "具体实现B的方法执行" << endl;
34     }
35 
36     virtual ~ConcreteImplementorB()
37     {
38     
39     }
40 };
41 
42 class Abstraction
43 {
44 protected:
45     Implementor* implementor;
46 public:
47     void SetImplementor(Implementor* implementor)
48     {
49         this->implementor = implementor;
50     }
51 
52     virtual void Operation()
53     {
54 
55     }
56 
57     virtual ~Abstraction()
58     {
59     
60     }
61 };
62 
63 class RefinedAbstraction : public Abstraction
64 {
65 public:
66     virtual void Operation()
67     {
68         implementor->Operation();
69     }
70 
71     virtual ~RefinedAbstraction()
72     {
73     
74     }
75 };
76 
77 void main()
78 {
79     Abstraction* ab = new RefinedAbstraction();
80 
81     ConcreteImplementorA* ca = new ConcreteImplementorA();
82     ab->SetImplementor(ca);
83     ab->Operation();
84 
85     ConcreteImplementorB* cb = new ConcreteImplementorB();
86     ab->SetImplementor(cb);
87     ab->Operation();
88 
89     delete ab;
90     ab = NULL;
91     delete ca;
92     ca = NULL;
93     delete cb;
94     cb = NULL;
95 
96 }

桥接模式特点

① 实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来让他们独立变化,减少他们之间的内聚

桥接模式实例应用

桥接模式实例应用类图

图 02 桥接模式实例应用类图

桥接模式实例应用代码

  1 #include "iostream"
  2 using namespace std;
  3 
  4 // 手机软件类
  5 class CHandsetSoft
  6 {
  7 public:
  8     virtual void Run() = 0;
  9     
 10     virtual ~CHandsetSoft()
 11     {
 12     
 13     }
 14 };
 15 
 16 
 17 // 手机游戏类
 18 class CHandsetGame : public CHandsetSoft
 19 {
 20 public:
 21     virtual void Run()
 22     {
 23         cout << "运行手机游戏" << endl;
 24     }
 25 
 26     virtual ~CHandsetGame()
 27     {
 28     
 29     }
 30 };
 31 
 32 // 手机通讯录类
 33 class CHandsetAddressList : public CHandsetSoft
 34 {
 35 public:
 36     virtual void Run()
 37     {
 38         cout << "运行手机通讯录" << endl;
 39     }
 40 
 41     virtual ~CHandsetAddressList()
 42     {
 43     
 44     }
 45 };
 46 
 47 // 手机品牌类
 48 class CHandsetBrand
 49 {
 50 protected:
 51     CHandsetSoft* soft;
 52 public:
 53     void SetHandsetSoft(CHandsetSoft* soft)
 54     {
 55         this->soft = soft;
 56     }
 57 
 58     virtual void Run()
 59     {
 60         soft->Run();
 61     }
 62 
 63     virtual ~CHandsetBrand()
 64     {
 65     
 66     }
 67 };
 68 
 69 // 手机品牌N类
 70 class CHandsetBrandN : public CHandsetBrand
 71 {
 72 public:
 73     virtual void Run()
 74     {
 75         soft->Run();
 76     }
 77 
 78     virtual ~CHandsetBrandN()
 79     {
 80     
 81     }
 82 };
 83 
 84 // 手机品牌M类
 85 class CHandsetBrandM : public CHandsetBrand
 86 {
 87 public:
 88     virtual void Run()
 89     {
 90         soft->Run();
 91     }
 92     virtual ~CHandsetBrandM()
 93     {
 94     
 95     }
 96 };
 97 
 98 void main()
 99 {
100     CHandsetBrand* ab = new CHandsetBrandN();
101     
102     CHandsetSoft* hg = new CHandsetGame();
103     ab->SetHandsetSoft(hg);
104     ab->Run();
105 
106     CHandsetSoft* hl = new CHandsetAddressList();
107     ab->SetHandsetSoft(hl);
108     ab->Run();
109 
110     delete ab;
111     ab = NULL;
112     delete hg;
113     hg = NULL;
114     delete hl;
115     hl = NULL;
116 }

2014-12-06    17:01:26

 

posted on 2014-12-06 16:57  xiaoheike  阅读(270)  评论(0)    收藏  举报

导航