设计模式学习<六>C++实现--bridge模式
结构型模式-bridge设计模式
bridge模式实现类图如下:
在如上类图中,refinedAbstraction类代表了windows操作系统,该类中维护了一个指向abstractImplement的指针,这样就可以自由的调用下面的具体的方法了。
如果用户突然想给windows系统增加一个计算器功能,这样我们就只需要给abstractimplement添加一个子类实现计算器的功能就行。
如果用户突然又想,实现mac操作系统,这样我们只需在abstract中添加一个mac的子类,这样mac类中就可以通过一个指向abstractimplement的指针实现上面的功能。
以上是我的理解。
参考代码:(来自http://blog.csdn.net/feiyinzilgd/article/details/6227664)
1 #pragma once 2 //Abstraction.h 3 class Abstraction 4 { 5 public: 6 Abstraction(void){}; 7 virtual ~Abstraction(void){}; 8 9 virtual void Operation() = 0; 10 };
1 #pragma once 2 3 //RefinedAbstraction.h 4 #include "abstraction.h" 5 #include "Implementor.h" 6 7 class Implementor; 8 9 class RefinedAbstraction : 10 public Abstraction 11 { 12 public: 13 RefinedAbstraction(void); 14 RefinedAbstraction(Implementor *imp); 15 ~RefinedAbstraction(void); 16 17 void Operation(); 18 19 private: 20 Implementor *m_imp; 21 22 };
1 #include <iostream> 2 #include "RefinedAbstraction.h" 3 4 using namespace std; 5 6 //RefinedAbstraction.cpp 7 RefinedAbstraction::RefinedAbstraction(void) 8 { 9 m_imp = NULL; 10 11 } 12 13 RefinedAbstraction::~RefinedAbstraction(void) 14 { 15 } 16 17 RefinedAbstraction::RefinedAbstraction(Implementor *imp) 18 { 19 m_imp = imp; 20 } 21 22 void RefinedAbstraction::Operation() 23 { 24 m_imp->OperationImp(); 25 }
1 #pragma once 2 //Implementor.h 3 class Implementor 4 { 5 public: 6 Implementor(void){}; 7 virtual ~Implementor(void){}; 8 9 virtual void OperationImp() = 0; 10 };
1 #pragma once 2 3 //ConcreteImplementorA.h 4 #include "implementor.h" 5 6 class ConcreteImplementorA : 7 public Implementor 8 { 9 public: 10 ConcreteImplementorA(void); 11 ~ConcreteImplementorA(void); 12 13 void OperationImp(); 14 };
1 #pragma once 2 3 //ConcreteImplementorB.h 4 #include "implementor.h" 5 6 class ConcreteImplementorB : 7 public Implementor 8 { 9 public: 10 ConcreteImplementorB(void); 11 ~ConcreteImplementorB(void); 12 13 void OperationImp(); 14 };
1 #include <iostream> 2 #include "ConcreteImplementorA.h" 3 4 using namespace std; 5 6 //ConcreteImplementonA.cpp 7 ConcreteImplementorA::ConcreteImplementorA(void) 8 { 9 } 10 11 ConcreteImplementorA::~ConcreteImplementorA(void) 12 { 13 } 14 15 void ConcreteImplementorA::OperationImp() 16 { 17 cout << "Hi, I am in ConcreteImplementorA" << endl; 18 }
1 #include <iostream> 2 #include "ConcreteImplementorB.h" 3 4 using namespace std; 5 6 7 //ConcreteImplementorB.cpp 8 ConcreteImplementorB::ConcreteImplementorB(void) 9 { 10 } 11 12 ConcreteImplementorB::~ConcreteImplementorB(void) 13 { 14 } 15 16 void ConcreteImplementorB::OperationImp() 17 { 18 cout << "Hi, I am in ConcreteImplementorB" << endl; 19 }
1 #include "Abstraction.h" 2 #include "ConcreteImplementorA.h" 3 #include "ConcreteImplementorB.h" 4 #include "RefinedAbstraction.h" 5 6 //main.cpp 7 int main() 8 { 9 10 ConcreteImplementorA *pImpA = new ConcreteImplementorA; 11 ConcreteImplementorB *pImpB = new ConcreteImplementorB; 12 Abstraction *objA = new RefinedAbstraction(pImpA); 13 Abstraction *objB = new RefinedAbstraction(pImpB); 14 15 objA->Operation(); 16 objB->Operation(); 17 18 }