这是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() = ; int SetMobileSoft(CMobileSoft* pSoft)
{
m_pMyMobileSoft = pSoft;
return ;
} //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 ;
}
}; #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 ;
}
}; #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() = ;
}; #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 ;
}
}; #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 ;
}
}; #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 ;
}

设计模式—桥接模式的C++实现的更多相关文章

  1. 转:设计模式-----桥接模式(Bridge Pattern)

    转自:http://www.cnblogs.com/houleixx/archive/2008/02/23/1078877.html 记得看原始链接的评论. 学习设计模式也有一段时间了,今天就把我整理 ...

  2. 跟着ZHONGHuan学习设计模式--桥接模式

    转载请注明出处! ! !http://blog.csdn.net/zhonghuan1992 全部配套代码均在github上:https://github.com/ZHONGHuanGit/Desig ...

  3. linkin大话设计模式--桥接模式

    linkin大话设计模式--桥接模式 桥接模式是一种结构化模式,他主要应对的是:由于实际的需要,某个类具有2个或者2个以上维度的变化,如果只是使用继承将无法实现功能,或者会使得设计变得相当的臃肿.我们 ...

  4. java设计模式——桥接模式

    一. 定义与类型 定义:将抽象部分与他的具体实现部分分离,使它们都可以独立的变化,通过组合的方式建立两个类之间的联系,而不是继承 类型:结构性. 二. 使用场景 (1) 抽象和具体实现之间增加更多的灵 ...

  5. 【设计模式】Java设计模式 - 桥接模式

    [设计模式]Java设计模式 - 桥接模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起 ...

  6. JAVA 设计模式 桥接模式

    用途 桥接模式 (Bridge) 将抽象部分与实现部分分离,使它们都可以独立的变化. 桥接模式是一种结构式模式. 结构

  7. javascript设计模式-桥接模式

    在系统中,某些类由于自身逻辑,具有两个或两个以上维度的变化,如何使得该类型可以沿多个方向变化,但又不引入额外的复杂度,这就是桥接模式要解决的问题. 定义:桥接模式(Bridge),将抽象部分与它的实现 ...

  8. 设计模式 -- 桥接模式(Bridge Pattern)

    桥接模式 Bridge Pattern 结构设计模式 定义: 分离抽象部分和实现部分,使他们独立运行. 避免使用继承导致系统类个数暴增,可以考虑桥接模式. 桥接模式将继承关系转化为关联关系,减少耦合, ...

  9. [Unity 设计模式]桥接模式(BridgePattern)

    1.前言 继上一讲IOC模式的基础上继续本讲桥接模式,笔者感觉桥接模式是23种设计模式中桥接模式是最好用但也是最难理解的设计模式之一,23中设计模式就好武侠剧中一本武功秘籍,我们在工作过程中想要熟练运 ...

  10. 结合JDK源码看设计模式——桥接模式

    前言: 在我们还没学习框架之前,肯定都学过JDBC.百度百科对JDBC是这样介绍的[JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Jav ...

随机推荐

  1. springboot项目部署云服务器

    Springboot项目部署云服务器 springboot项目部署云服务器还是挺简单的 首先你要有java运行环境,就是jdk的安装,如果还没有装没有参考安装:阿里云ECS建网站(建站)超详细全套完整 ...

  2. 译《The Part-Time Parliament》——终于读懂了Paxos协议!

    最近的考古发现表明,在Paxos小岛上,尽管兼职议会成员都有逍遥癖,但议会模式仍然起作用.他们依旧保持了一致的会议记录,尽管他们频繁的进出会议室并且他们的信使还很健忘.Paxon议会协议提供了一种新方 ...

  3. Chapter 4 Invitations——4

    I wanted very much to talk to him, and the day after the accident I tried. 在发生事故之后我尽力尝试,我很想和他聊聊. The ...

  4. httpd的编译安装

    1.环境介绍 系统:2.6.32-279.el6.i686 2.准备编译环境 [root@localhost ~]# yum groupinstall "Server Platform De ...

  5. springboot情操陶冶-SpringApplication(二)

    承接前文springboot情操陶冶-SpringApplication(一),本文将对run()方法作下详细的解析 SpringApplication#run() main函数经常调用的run()方 ...

  6. SpringBoot快速引入第三方jar包

    工作中,我们常会用到第三方jar包,而这些jar包往往在maven仓库是搜不到的,下面推荐一种简单.快速的引入第三方依赖的方法: 比如第三方jar包在lib文件夹下,对pom.xml的配置如下: &l ...

  7. Perl的输出:print、say和printf、sprintf

    print.printf和say都可以输出信息.print和say类似,print不自带换行符,say自带换行符,但要使用say,必须写use语句use 5.010;,printf像C语言的print ...

  8. 数据库部分(MySql)_3

    表设计之关联关系 一对一:有两张表A和B,A表中有一条数据对应B表中的一条数据称为一对一: 应用场景:用户表和用户扩展表,商品表和商品信息扩展表: 如何建立关系:在从表中添加一个外键字段指向主表的主键 ...

  9. mysql+mycat实现读写分离

    centos7       master slave mycat1.6 client 192.168.41.10 192.168.41.11 192.168.41.12 192.168.41.13 实 ...

  10. 【Java每日一题】20170207

    20170206问题解析请点击今日问题下方的“[Java每日一题]20170207”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...