Bridge Pattern
1.Bridge Pattern :使用组合的方式将“抽象”和“实现”彻底的解耦。这里的实现不是指继承基类,实现基类接口,而是指通过对象的组合实现用户的需求。
面向对象分析和设计中的原则: Favor Compsition Over Inheritance.
2.Bridge Pattern 模式结构图
3.实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "Abstraction.h" 2 #include "AbstractionImp.h" 3 #include <iostream> 4 using namespace std; 5 6 int main(int argc,char* argv[]) 7 { 8 AbstractionImp* imp = new ConcreteAbstractionImpA(); 9 Abstraction* abs = new RefinedAbstraction(imp); 10 abs->Operation(); 11 return 0; 12 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef _ABSTRACTION_H_ 2 #define _ABSTRACTION_H_ 3 class AbstractionImp; 4 5 class Abstraction 6 { 7 public: 8 virtual ~Abstraction(); 9 virtual void Operation() = 0; 10 protected: 11 Abstraction(); 12 private: 13 }; 14 15 class RefinedAbstraction:public Abstraction 16 { 17 public: 18 RefinedAbstraction(AbstractionImp* imp); 19 ~RefinedAbstraction(); 20 void Operation(); 21 protected: 22 private: 23 AbstractionImp* _imp; 24 }; 25 26 #endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "Abstraction.h" 2 #include "AbstractionImp.h" 3 #include <iostream> 4 using namespace std; 5 6 Abstraction::Abstraction() 7 { 8 9 } 10 Abstraction::~Abstraction() 11 { 12 13 } 14 RefinedAbstraction::RefinedAbstraction(AbstractionImp* imp) 15 { 16 _imp = imp; 17 } 18 RefinedAbstraction::~RefinedAbstraction() 19 { 20 21 } 22 void RefinedAbstraction::Operation() 23 { 24 _imp->Operation(); 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef _ABSTRACTIONIMP_H_ 2 #define _ABSTRACTIONIMP_H_ 3 4 class AbstractionImp 5 { 6 public: 7 virtual ~AbstractionImp(); 8 virtual void Operation() = 0; 9 protected: 10 AbstractionImp(); 11 private: 12 }; 13 14 class ConcreteAbstractionImpA:public AbstractionImp 15 { 16 public: 17 ConcreteAbstractionImpA(); 18 ~ConcreteAbstractionImpA(); 19 virtual void Operation(); 20 protected: 21 private: 22 }; 23 24 class ConcreteAbstractionImpB:public AbstractionImp 25 { 26 public: 27 ConcreteAbstractionImpB(); 28 ~ConcreteAbstractionImpB(); 29 virtual void Operation(); 30 protected: 31 private: 32 }; 33 34 #endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "AbstractionImp.h" 2 #include <iostream> 3 using namespace std; 4 5 AbstractionImp::AbstractionImp() 6 { 7 8 } 9 AbstractionImp::~AbstractionImp() 10 { 11 12 } 13 void AbstractionImp::Operation() 14 { 15 cout<<"AbstractionImp....imp..."<<endl; 16 } 17 ConcreteAbstractionImpA::ConcreteAbstractionImpA() 18 { 19 20 } 21 ConcreteAbstractionImpA::~ConcreteAbstractionImpA() 22 { 23 24 } 25 void ConcreteAbstractionImpA::Operation() 26 { 27 cout<<"ConcreteAbstractionImpA...."<<endl; 28 } 29 ConcreteAbstractionImpB::ConcreteAbstractionImpB() 30 { 31 32 } 33 ConcreteAbstractionImpB::~ConcreteAbstractionImpB() 34 { 35 36 } 37 void ConcreteAbstractionImpB::Operation() 38 { 39 cout<<"ConcreteAbstractionImpB...."<<endl; 40 }