现在的位置: 首页 > 综合 > 正文

设计模式—-Bridge模式

2013年03月31日 ⁄ 综合 ⁄ 共 2902字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/fly542/article/details/6720217

原来对bridge模式理解不是很深入,感觉和build模式很相似,今天又看了四人帮的关于bridge模式的描述,有些新的理解

先来说下适用性

1、不想抽象和实现之间有一个固定的绑定关系。(因为程序在运行时实现部分可以被选择或者切换)。

2、类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对他们进行扩充。(常用)

3、对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。

4、你想对客户完全以藏抽象的实现部分。在c++中,类的表示在类接口中是可见的。

5、在多个对象间共享实现(可能是引用计数), 但同时要求客户并不知道这一点。

还有一种方式没理解,书上是这么说的:正如在意图一节的类图中所示的那样,有许多类要生成。这样一种类层次结构说明你必须将一个对象分解城两个部分。这种层次结构为“嵌套的普化”(nestd generalizations)***

 

结构图如下

 

示例代码如下

  1. #include <QtCore/QCoreApplication>  
  2.   
  3. #include <iostream>  
  4.   
  5. class Implementor;  
  6.   
  7. // 维护一个Implementor类的指针  
  8. class Abstraction  
  9. {  
  10. public:  
  11.     Abstraction(Implementor* pImplementor);  
  12.     virtual ~Abstraction();  
  13.   
  14.     void Operation();  
  15.   
  16. protected:  
  17.     Implementor* m_pImplementor;  
  18. };  
  19.   
  20. // 为实现Abstraction定义的抽象基类,定义了实现的接口函数  
  21. class Implementor  
  22. {  
  23. public:  
  24.     Implementor(){}  
  25.     virtual ~Implementor(){}  
  26.   
  27.     virtual void OperationImpl() = 0;  
  28. };  
  29.   
  30. // 继承自Implementor,是Implementor的不同实现之一  
  31. class ConcreateImplementorA  
  32.     : public Implementor  
  33. {  
  34. public:  
  35.     ConcreateImplementorA(){}  
  36.     virtual ~ConcreateImplementorA(){}  
  37.   
  38.     virtual void OperationImpl();  
  39. };  
  40.   
  41. // 继承自Implementor,是Implementor的不同实现之一  
  42. class ConcreateImplementorB  
  43.     : public Implementor  
  44. {  
  45. public:  
  46.     ConcreateImplementorB(){}  
  47.     virtual ~ConcreateImplementorB(){}  
  48.   
  49.     virtual void OperationImpl();  
  50. };  
  51.   
  52. void ConcreateImplementorA::OperationImpl()  
  53. {  
  54.     std::cout << "Implementation by ConcreateImplementorA\n";  
  55. }  
  56.   
  57. void ConcreateImplementorB::OperationImpl()  
  58. {  
  59.     std::cout << "Implementation by ConcreateImplementorB\n";  
  60. }  
  61.   
  62. Abstraction::Abstraction(Implementor* pImplementor)  
  63.     : m_pImplementor(pImplementor)  
  64. {  
  65. }  
  66.   
  67. Abstraction::~Abstraction()  
  68. {  
  69. }  
  70.   
  71. void Abstraction::Operation()  
  72. {  
  73.     m_pImplementor->OperationImpl();  
  74. }  
  75.   
  76. template <typename deleteType>  
  77. inline void deleteObject(const deleteType * obj)  
  78. {  
  79.     delete obj;  
  80.     obj = 0;  
  81. }  
  82.   
  83. int main(int argc, char *argv[])  
  84. {  
  85.     QCoreApplication a(argc, argv);  
  86.   
  87.     ConcreateImplementorA *pImplA = new ConcreateImplementorA();  
  88.     Abstraction *pAbstraction1 = new Abstraction(pImplA);  
  89.     pAbstraction1->Operation();  
  90.   
  91.     ConcreateImplementorB *pImplB = new ConcreateImplementorB();  
  92.     Abstraction *pAbstraction2 = new Abstraction(pImplB);  
  93.     pAbstraction2->Operation();  
  94.   
  95.     deleteObject(pImplB);  
  96.     deleteObject(pAbstraction2);  
  97.     deleteObject(pImplA);  
  98.     deleteObject(pAbstraction1);  
  99.   
  100.   
  101.     return a.exec();  
  102. }  


 

因为Adapter中包含了一个Adaptee对象,这是一个聚合或者组合的关系。而且也是在Adapter的request方法中调用了Adaptee对象中的方法,从这个角度而言,Adapter模式和Bridge模式是非常类似的。

 

但是,他们之间有本质的区别:

1.       在Adapter模式中,Adaptee本身往往已经是一个具体的、已经存在的类。在Bridge模式中,Implementor则是一个抽象类或者接口;

2.       在Adapter模式中,Adapter类也是一个具体的类。在Bridge模式中,Abstraction则是一个抽象类;

3.       在Adapter模式中,Adapter类派生于一个抽象类/接口(客户程序所期望的)。在Bridge模式中,Abstraction类则不存在这样的情况。

4.       最本质同时也是最重要的区别是,它们的意图是不同的。

抱歉!评论已关闭.