设计模式笔记--结构型模式之二--桥接 Bridge

意图:

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

适用性:

1不希望在抽象和它的实现部分之间有一个固定的绑定关系

2类的抽象以及它的实现方法都应该可以通过生成子类的方法加以扩充

3对一个抽象的实现部分的修改应对客户不产生影响

4对客户完全隐藏抽象的实现部分

 

效果:

1分离接口及其实现部分 一个实现未必不变地绑定在一个接口上,抽象类的实现可以在运行时刻进行配置

2提高可扩充性 可独立的对AbstractionImplementation层次结构进行扩充

3实现细节对客户透明

 

实现:

1仅有一个Implementor 没有必要创建一个抽象的Implementor

2创建正确的Implementor对象

3共享Implementor对象

4采用多重继承机制,可以将抽象接口和它的实现部分结合起来

 

其示例代码如下:

Bridge.h

  1. #include <iostream>
  2. using namespace std;
  3. class WindowImp
  4. {
  5. public:
  6.     virtual void DeviceRect(int,int,int,int)=0;
  7.     WindowImp(){cout<<"XWindowImp is called"<<endl;}
  8. };
  9. class Window
  10. {
  11. public:
  12.     Window();
  13.     virtual void DrawContents();
  14.     virtual WindowImp* GetWindowImp(int i);
  15. //private:
  16.     WindowImp* _imp;
  17. };
  18. class ApplicationWindow: public Window
  19. {
  20. public:
  21.     virtual void DrawContents();
  22. };
  23. class XWindowImp:public WindowImp
  24. {
  25. public:
  26.     XWindowImp(){cout<<"XWindowImp is called"<<endl;}
  27.     virtual void DeviceRect(int,int,int,int)
  28.     {cout<<"Draw DeviceRect on XWindowImp"<<endl;}
  29. };
  30. class PMWindowImp:public WindowImp
  31. {
  32. public:
  33.     PMWindowImp(){cout<<"PMWindowImp is called"<<endl;}
  34.     virtual void DeviceRect(int,int,int,int)
  35.     {
  36.      cout<<"Draw DeviceRect on PMWindowImp"<<endl;
  37.     }
  38. };
  39. class WindowSystemFactory
  40. {
  41. public:
  42.     static WindowSystemFactory* Instance();
  43.     WindowImp*  MakeWindowImp(int i);
  44.     WindowSystemFactory();
  45. private:
  46.     static WindowSystemFactory* _instance;
  47. };

Bridge.cpp

  1. #include "Bridge.h"
  2. #include <iostream>
  3. using namespace std;
  4. Window::Window()
  5. {cout<<"A window is created."<<endl;
  6.  _imp = 0;
  7. }
  8. void Window::DrawContents()
  9. {cout<<"Draw contents on the window"<<endl;}
  10. void ApplicationWindow::DrawContents()
  11. {cout<<"Draw contents on the application window"<<endl;}
  12. WindowImp* Window::GetWindowImp(int i)
  13. {
  14.     if(_imp == 0)
  15.     {
  16.         //_imp = WindowSystemFactory::Instance()->MakeWindowImp(i);
  17.         _imp = new XWindowImp();
  18.     }
  19.     cout<<"IMP is returned"<<endl;
  20.     //_imp->DeviceRect(1,2,3,4);
  21.     return _imp;
  22. /*  _imp = new XWindowImp();
  23.     return _imp;
  24. */
  25. }
  26. WindowSystemFactory::WindowSystemFactory()
  27. {
  28.     cout<<"A WindowSystemFactory is created"<<endl;
  29. }
  30. WindowSystemFactory* WindowSystemFactory::_instance =0;
  31. WindowSystemFactory* WindowSystemFactory::Instance()
  32. {
  33.     //cout<<"Enter an instance."<<endl;
  34.     if(_instance == 0)
  35.     {
  36.         _instance = new WindowSystemFactory();
  37.         cout<<"An instance is created successfully."<<endl;
  38.     }
  39.     else
  40.         cout<<"An instance is already existed."<<endl;
  41.     return _instance;
  42. }
  43. WindowImp*  WindowSystemFactory::MakeWindowImp(int i)
  44. {
  45.     switch (i)
  46.     {
  47.     case 0:
  48.         return new XWindowImp;
  49.     case 1: 
  50.         return new PMWindowImp;
  51.     default:
  52.         return new PMWindowImp;
  53.     }
  54. }

main.cpp

 

  1. #include "Bridge.h"
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     ApplicationWindow a;
  7.     WindowImp* c;
  8.     WindowImp* d;
  9.     d = new XWindowImp;
  10.     d->DeviceRect(1,2,3,4);
  11.     c=a.GetWindowImp(1);
  12.     c->DeviceRect(1,2,3,4);
  13.     
  14.     return 0;
  15. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值