设计模式——桥接模式(C++实现)
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
相关推荐
AscaryBird 2020-07-27
zcabcd 2020-04-30
summerinsist 2020-04-30
leeham 2020-04-19
Wytheme 2020-01-19
风吹草动 2019-12-18
水痕 2019-12-06
89284553 2019-12-04
xuebingnan 2019-10-23
87324554 2012-09-04
hce0 2015-12-05
小波波 2011-06-15
易分享 2015-05-06
wwwchinabignet 2011-07-08
麦田开拓者 2011-07-05
Palingenesis 2019-07-01
MoreWindowsBlog 2019-06-28
苗疆三刀的随手记 2019-06-28