设计模式—桥接模式的C++实现
这是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; }
作者:Bwar 出处:https://www.cnblogs.com/bwar/
Bwar倾力打造的高性能网络框架Nebula:https://github.com/Bwar/Nebula
原创文章如转载,请注明出处。本文首发于博客园。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
java之JMS
一、简介:JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持。 二、JMS对象模型包含如下几个要素: 1)连接工厂。连接工厂(ConnectionFactory)是由管理员创建,并绑定到 JNDI树中。客户端使用JNDI查找连接工厂,然后利用连接工厂创建一个JMS连接。 2)JMS连接。JMS连接(Connection)表示JMS客户端和服务器端之间的一个活动的连接,是由客户端通过调用连接工厂的方法建立的。 3)JMS会话。JMS会话(Session)表示JMS客户与JMS服务器之间的会话状态。JMS会话建立在JMS连接上,表示客户与服务器之间的一个会话线程。 4)JMS目的。JMS目的(Destination),又称为 消息队列,是实际的消息源。 5)JMS生产者和消费者。生产者(Message Producer)和消费者(Message Consu...
- 下一篇
从 0 到 1 搭建 webpack2+vue2 自定义模板详细教程
前言 webpack2和vue2已经不是新鲜东西了,满大街的文章在讲解webpack和vue,但是很多内容写的不是很详细,对于很多个性化配置还是需要自己过一遍文档。Vue官方提供了多个vue-templates,基于vue-cli用的最多,不过对于很多人来说,vue-cli 的配置还是过于复杂,对于我们了解细节实现不是很好,所以想自己从零开始搭建一个模板工程,也顺便重新认识一下webpack和vue工程化的细节。 webpack 核心概念 Webpack 是当下最热门的前端资源模块化管理和打包工具。它可以将许多松散的模块按照依赖和规则打包成符合生产环境部署的前端资源。还可以将按需加载的模块进行代码分隔,等到实际需要的时候再异步加载。通过 loader 的转换,任何形式的资源都可以视作模块,比如 CommonJs 模块、 AMD 模块、 ES6 模块、CSS、图片、 JSON、Coffeescript、 LESS 等。 官方网站:webpack.js.org/ 安装 在开始前,先要确认你已经安装Node.js的最新版本。使用 Node.js 最新的 LTS 版本,是理想的起步。使用旧版本...
相关文章
文章评论
共有0条评论来说两句吧...