[设计模式]Bridge桥接模式

目录(?)[+]

Bridge桥接模式

将抽象部分与它的实现部分分离,使它们都可以独立地变化。

解析

1.Abastrction:某个抽象类,它的实现方式有Implementor完成

2.Implementor:实现类的抽象基类,定义了实现Abstraction的基本操作,而它的派生类实现这些接口

3.Implementor::OperationImp:定义了为实现Abstract需要的基本操作,有Implementor的派生类实现,而在Abstraction::operation函数中根据不同的指针多态调用这个函数

Bridge用于将表示和实现解耦,两者可以独立的变化。在Abstraction类中维护一个Implementor类指针,需要采用不同的实现方式的时候只需要传入不同的Implementor派生类就可以了。

Bridge的实现方式其实和Builder十分相近,可以这么说:本质上是一样的,只是封装的东西不一样罢了。

两者的实现都有如下的共同点:

  • 抽象出来一个基类,这个基类里面定义了共有的一些行为,形成接口函数(对接口编程而不是对实现编程),这个接口函数在Builder中是BuilderPart函数,在Bridge模式中是OperationImp函数。
  • 其次,聚合一个基类的指针,如Builder模式中Director类聚合了一个Builder基类的指针,而Bridge模式中Abstraction类聚合了一个Implementor基类的指针(优先采用聚合而不是继承)
  • 而在使用的时候,都把对这个类的使用封装在一个函数中,在Builder中是封装在Director::Construct函数中,因为装配不同部分的过程是一致的,而在Bridge模式中则是封装在Abstraction::Operation函数中,在这个函数中调用调用对应的Implementor::operationImp函数。
  • 就这两个模式而言,Builder封装了不同的生成组成部分的方式,而Bridge模式封装了不同的实现方式。因此,如果以一些最基本的面向对象的设计原则来分析这些模式的实现的话,还是可以看到很多共同的地方的。

小demo

bridge.h

[cpp]  view plain  copy
  1. #ifndef BRIDEG_H  
  2. #define BRIDEG_H  
  3.   
  4. class Implementor;  
  5.   
  6. // 维护一个Implementor类的指针  
  7. class Abstraction  
  8. {  
  9. public:  
  10.     Abstraction(Implementor* pImplementor);  
  11.     virtual ~Abstraction();  
  12.   
  13.     void Operation();  
  14. protected:  
  15.     Implementor* m_pImplementor;  
  16. };  
  17.   
  18. // 为实现Abstraction定义的抽象基类,定义了实现的接口函数  
  19. class Implementor  
  20. {  
  21. public:  
  22.     Implementor(){}  
  23.     virtual ~Implementor(){}  
  24.   
  25.     virtual void OperationImpl() = 0;  
  26. };  
  27.   
  28. // 继承自Implementor,是Implementor的不同实现之一  
  29. class ConcreateImplementorA : public Implementor  
  30. {  
  31. public:  
  32.     ConcreateImplementorA(){}  
  33.     virtual ~ConcreateImplementorA(){}  
  34.   
  35.     virtual void OperationImpl();  
  36. };  
  37.   
  38. // 继承自Implementor,是Implementor的不同实现之一  
  39. class ConcreateImplementorB : public Implementor  
  40. {  
  41. public:  
  42.     ConcreateImplementorB(){}  
  43.     virtual ~ConcreateImplementorB(){}  
  44.   
  45.     virtual void OperationImpl();  
  46. };  
  47.   
  48. #endif  

bridge.cpp

[cpp]  view plain  copy
  1. #include "Brige.h"  
  2. #include <iostream>  
  3.   
  4. void ConcreateImplementorA::OperationImpl()  
  5. {  
  6.     std::cout << "Implementation by ConcreateImplementorA\n";  
  7. }  
  8.   
  9. void ConcreateImplementorB::OperationImpl()  
  10. {  
  11.     std::cout << "Implementation by ConcreateImplementorB\n";  
  12. }  
  13.   
  14. Abstraction::Abstraction(Implementor* pImplementor) : m_pImplementor(pImplementor)  
  15. {  
  16. }  
  17.   
  18. Abstraction::~Abstraction()  
  19. {  
  20.     delete m_pImplementor;  
  21.     m_pImplementor = NULL;  
  22. }  
  23.   
  24. void Abstraction::Operation()  
  25. {  
  26.     m_pImplementor->OperationImpl();  
  27. }  

main.cpp

[cpp]  view plain  copy
  1. #include "Brige.h"  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. int main()  
  5. {  
  6.     Implementor *pImplA = new ConcreateImplementorA();  
  7.     Abstraction *pAbstraction1 = new Abstraction(pImplA);  
  8.     pAbstraction1->Operation();  
  9.   
  10.     Implementor *pImplB = new ConcreateImplementorB();  
  11.     Abstraction *pAbstraction2 = new Abstraction(pImplB);  
  12.     pAbstraction2->Operation();  
  13.   
  14.     delete pAbstraction1;  
  15.     delete pAbstraction2;  
  16.   
  17.     system("pause");  
  18.     return 0;  
  19. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值