參考: http://www.cnblogs.com/jiese/p/3164940.html http://design-patterns.readthedocs.io/zh_CN/latest/structural_patterns/bridge.html ...
1 [root@ ~/learn_code/design_pattern/18_bridge]$ cat Abstraction.h 2 #ifndef _ABSTRACTION_H_ 3 #define _ABSTRACTION_H_ 4 5 class AbstractionImplement; 6 7 class Abstraction 8 { 9 public: 10 virtual void Operation()=0;//定義介面,表示該類所支持的操作 11 virtual ~Abstraction(); 12 protected: 13 Abstraction(); 14 }; 15 16 class RefinedAbstractionA:public Abstraction 17 { 18 public: 19 RefinedAbstractionA(AbstractionImplement* imp);//構造函數 20 virtual void Operation();//實現介面 21 virtual ~RefinedAbstractionA();//析構函數 22 private: 23 AbstractionImplement* _imp;//私有成員 24 }; 25 26 class RefinedAbstractionB:public Abstraction 27 { 28 public: 29 RefinedAbstractionB(AbstractionImplement* imp);//構造函數 30 virtual void Operation();//實現介面 31 virtual ~RefinedAbstractionB();//析構函數 32 private: 33 AbstractionImplement* _imp;//私有成員 34 }; 35 #endif 36 37 38 [root@ ~/learn_code/design_pattern/18_bridge]$ cat Abstraction.cpp 39 #include "Abstraction.h" 40 #include "AbstractionImplement.h" 41 #include <iostream> 42 43 using namespace std; 44 45 Abstraction::Abstraction() 46 {} 47 48 Abstraction::~Abstraction() 49 {} 50 51 RefinedAbstractionA::RefinedAbstractionA(AbstractionImplement* imp) 52 { 53 this->_imp = imp; 54 } 55 56 RefinedAbstractionA::~RefinedAbstractionA() 57 { 58 delete this->_imp; 59 this->_imp = NULL; 60 } 61 62 void RefinedAbstractionA::Operation() 63 { 64 cout << "RefinedAbstractionA::Operation" << endl; 65 this->_imp->Operation(); 66 } 67 68 RefinedAbstractionB::RefinedAbstractionB(AbstractionImplement* imp) 69 { 70 this->_imp = imp; 71 } 72 73 RefinedAbstractionB::~RefinedAbstractionB() 74 { 75 delete this->_imp; 76 this->_imp = NULL; 77 } 78 79 void RefinedAbstractionB::Operation() 80 { 81 cout << "RefinedAbstractionB::Operation" << endl; 82 this->_imp->Operation(); 83 } 84 85 86 [root@ ~/learn_code/design_pattern/18_bridge]$ cat AbstractionImplement.h 87 #ifndef _ABSTRACTIONIMPLEMENT_H_ 88 #define _ABSTRACTIONIMPLEMENT_H_ 89 90 //抽象基類,定義了實現的介面 91 class AbstractionImplement 92 { 93 public: 94 virtual void Operation()=0;//定義操作介面 95 virtual ~AbstractionImplement(); 96 protected: 97 AbstractionImplement(); 98 }; 99 100 // 繼承自AbstractionImplement,是AbstractionImplement的不同實現之一 101 class ConcreteAbstractionImplementA:public AbstractionImplement 102 { 103 public: 104 ConcreteAbstractionImplementA(); 105 void Operation();//實現操作 106 ~ConcreteAbstractionImplementA(); 107 protected: 108 }; 109 110 // 繼承自AbstractionImplement,是AbstractionImplement的不同實現之一 111 class ConcreteAbstractionImplementB:public AbstractionImplement 112 { 113 public: 114 ConcreteAbstractionImplementB(); 115 void Operation();//實現操作 116 ~ConcreteAbstractionImplementB(); 117 protected: 118 }; 119 #endif 120 121 122 [root@ ~/learn_code/design_pattern/18_bridge]$ cat AbstractionImplement.cpp 123 #include "AbstractionImplement.h" 124 #include <iostream> 125 126 using namespace std; 127 128 AbstractionImplement::AbstractionImplement() 129 {} 130 131 AbstractionImplement::~AbstractionImplement() 132 {} 133 134 ConcreteAbstractionImplementA::ConcreteAbstractionImplementA() 135 {} 136 137 ConcreteAbstractionImplementA::~ConcreteAbstractionImplementA() 138 {} 139 140 void ConcreteAbstractionImplementA::Operation() 141 { 142 cout << "ConcreteAbstractionImplementA Operation" << endl; 143 } 144 145 ConcreteAbstractionImplementB::ConcreteAbstractionImplementB() 146 {} 147 148 ConcreteAbstractionImplementB::~ConcreteAbstractionImplementB() 149 {} 150 151 void ConcreteAbstractionImplementB::Operation() 152 { 153 cout << "ConcreteAbstractionImplementB Operation" << endl; 154 } 155 156 157 [root@ ~/learn_code/design_pattern/18_bridge]$ cat main.cpp 158 #include "Abstraction.h" 159 #include "AbstractionImplement.h" 160 #include <iostream> 161 162 using namespace std; 163 164 int main() 165 { 166 /* 將抽象部分與它的實現部分分離,使得它們可以獨立地變化 167 168 1、抽象Abstraction與實現AbstractionImplement分離; 169 170 2、抽象部分Abstraction可以變化,如new RefinedAbstractionA(imp)、new RefinedAbstractionB(imp2); 171 172 3、實現部分AbstractionImplement也可以變化,如new ConcreteAbstractionImplementA()、new ConcreteAbstractionImplementB(); 173 174 */ 175 176 AbstractionImplement* imp = new ConcreteAbstractionImplementA(); //實現部分ConcreteAbstractionImplementA 177 Abstraction* abs = new RefinedAbstractionA(imp); //抽象部分RefinedAbstractionA 178 abs->Operation(); 179 180 cout << "-----------------------------------------" << endl; 181 182 AbstractionImplement* imp1 = new ConcreteAbstractionImplementB(); //實現部分ConcreteAbstractionImplementB 183 Abstraction* abs1 = new RefinedAbstractionA(imp1); //抽象部分RefinedAbstractionA 184 abs1->Operation(); 185 186 cout << "-----------------------------------------" << endl; 187 188 AbstractionImplement* imp2 = new ConcreteAbstractionImplementA(); //實現部分ConcreteAbstractionImplementA 189 Abstraction* abs2 = new RefinedAbstractionB(imp2); //抽象部分RefinedAbstractionB 190 abs2->Operation(); 191 192 cout << "-----------------------------------------" << endl; 193 194 AbstractionImplement* imp3 = new ConcreteAbstractionImplementB(); //實現部分ConcreteAbstractionImplementB 195 Abstraction* abs3 = new RefinedAbstractionB(imp3); //抽象部分RefinedAbstractionB 196 abs3->Operation(); 197 198 cout << endl; 199 return 0; 200 }
參考:
http://www.cnblogs.com/jiese/p/3164940.html
http://design-patterns.readthedocs.io/zh_CN/latest/structural_patterns/bridge.html