C++ Bridge 设计模式

C++ Bridge 设计模式

 

Bridge模式是应用最广泛也是我个人比较喜欢的一个C++设计模式之一。

Bridge模式意图:降低抽象和实现之间的耦合。

面向对象系统设计和分析中最重要的一点就是:高内聚,低耦合。Bridge模式使得抽象和实现能够相对独立。

 

 

Bridge模式结构图:

 

        

             

 

Bridge模式的关键在于Abstraction的派生类中维护了一个纸箱Implementor类的指针。

让Implentor的派生类来去实现。从而实现了抽象和实现的相对独立。

 

参考代码:

 

  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. //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. #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 "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 <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. #include "Abstraction.h"  
  2. #include "ConcreteImplementorA.h"  
  3. #include "ConcreteImplementorB.h"  
  4. #include "RefinedAbstraction.h"  
  5.   
  6.   
  7. //main.cpp  
  8. int main()  
  9. {  
  10.   
  11.     ConcreteImplementorA *pImpA = new ConcreteImplementorA;  
  12.     ConcreteImplementorB *pImpB = new ConcreteImplementorB;  
  13.     Abstraction *objA = new RefinedAbstraction(pImpA);  
  14.     Abstraction *objB = new RefinedAbstraction(pImpB);  
  15.   
  16.     objA->Operation();  
  17.     objB->Operation();  
  18.       
  19. }  

 

 

 

我个人 非常欣赏也非常推崇Bridge模式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值