您现在的位置是:首页 > 文章详情

设计模式—桥接模式的C++实现

日期:2018-06-15点击:539

这是Bwar在2009年写的设计模式C++实现,代码均可编译可运行,一直存在自己的电脑里,曾经在团队技术分享中分享过,现搬到线上来。

1. 装饰模式简述

1.1 目的

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

1.2 适用性

     (1)  不希望抽象部分与实现部分之间有一个固定的绑定关系 ,在运行时刻实现部分可以被选择或切换。

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

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

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

2. 装饰模式结构图

  • Abstraction:定义抽象类的接口;维护一个只想Implementor类型对象的指针。
  • RefinedAbstraction:扩充由Abstraction定义的接口。
  • Implementor:定义实现类的接口,该接口不一定要与Abstraction的接口完全一致;事实上这两个接口可以完全不同。
  • ConcreteImplementor:实现Implementor接口并定义它的具体实现。

3.  桥接模式C++实现示例

      手机与手机软件的实现。

 

 代码实现:

Mobile.hpp:

#ifndef Mobile_HPP_ #define Mobile_HPP_ #include "MobileSoft.hpp" class CMobile { public: CMobile(){}; virtual ~CMobile(){} virtual int Run() = 0; int SetMobileSoft(CMobileSoft* pSoft) { m_pMyMobileSoft = pSoft; return 0; } //int Start(); //int Shutdown(); protected: CMobileSoft* GetMobileSoft() { return m_pMyMobileSoft; } private: CMobileSoft* m_pMyMobileSoft; unsigned int m_uiShape; unsigned int m_uiColour; }; #endif /* Mobile_HPP_ */

Nokia.hpp:

#ifndef NOKIA_HPP_ #define NOKIA_HPP_ #include "Mobile.hpp" class CNokia : public CMobile { public: CNokia(){}; virtual ~CNokia(){}; virtual int Run() { cout << "Nokia "; GetMobileSoft()->ImpRun(); return 0; } }; #endif /* NOKIA_HPP_ */

Moto.hpp:

#ifndef MOTO_HPP_ #define MOTO_HPP_ #include "Mobile.hpp" class CMoto : public CMobile { public: CMoto(){}; virtual ~CMoto(){}; virtual int Run() { cout << "Moto "; GetMobileSoft()->ImpRun(); return 0; } }; #endif /* MOTO_HPP_ */

MobileSoft.hpp:

#ifndef MobileSOFT_HPP_ #define MobileSOFT_HPP_ #include <cstdio> #include <iostream> using namespace std; class CMobileSoft { public: CMobileSoft(){}; virtual ~CMobileSoft(){}; virtual int ImpRun() = 0; }; #endif /* MobileSOFT_HPP_ */

MobileAddressList.hpp:

#ifndef MobileADDRESSLIST_HPP_ #define MobileADDRESSLIST_HPP_ #include "MobileSoft.hpp" class CMobileAddressList : public CMobileSoft { public: CMobileAddressList(){}; virtual ~CMobileAddressList(){}; virtual int ImpRun() { cout << "Mobile address list." << endl; return 0; } }; #endif /* MobileADDRESSLIST_HPP_ */

MobileGame.hpp:

#ifndef MobileGAME_HPP_ #define MobileGAME_HPP_ #include "MobileSoft.hpp" class CMobileGame : public CMobileSoft { public: CMobileGame(){}; virtual ~CMobileGame(){}; virtual int ImpRun() { cout << "Mobile game." << endl; return 0; } }; #endif /* MobileGAME_HPP_ */

BridgeMain.cpp:

#include <ctime> #include <iostream> #include "Mobile.hpp" #include "Nokia.hpp" #include "Moto.hpp" #include "MobileSoft.hpp" #include "MobileGame.hpp" #include "MobileAddressList.hpp" using namespace std; int main() { CMobile* pMyMobile; CMobileSoft* pMySoft; pMyMobile = new CNokia; pMySoft = new CMobileGame; pMyMobile->SetMobileSoft(pMySoft); pMyMobile->Run(); delete pMyMobile; delete pMySoft; return 0; }

https://github.com/Bwar/Nebula

 

作者:Bwar 出处:https://www.cnblogs.com/bwar/

Bwar倾力打造的高性能网络框架Nebula:https://github.com/Bwar/Nebula

原创文章如转载,请注明出处。本文首发于博客园。

原文链接:https://yq.aliyun.com/articles/612493
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章