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

简介: 这是Bwar在2009年写的设计模式C++实现,代码均可编译可运行,一直存在自己的电脑里,曾经在团队技术分享中分享过,现搬到线上来。1. 装饰模式简述1.1 目的      将抽象部分与它的实现部分分离,使它们可以独立地变化。

这是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

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

目录
相关文章
|
15小时前
|
设计模式 JavaScript Java
[设计模式Java实现附plantuml源码~结构型]处理多维度变化——桥接模式
[设计模式Java实现附plantuml源码~结构型]处理多维度变化——桥接模式
|
15小时前
|
设计模式 存储 Java
C++从入门到精通:3.5设计模式——提升代码可维护性与可扩展性的关键
C++从入门到精通:3.5设计模式——提升代码可维护性与可扩展性的关键
|
15小时前
|
设计模式
【设计模式系列笔记】桥接模式
桥接模式(Bridge Pattern)是一种结构性设计模式,它将抽象部分与实现部分分离,使它们可以独立变化而互不影响。桥接模式通过组合而不是继承的方式来实现这种分离。
31 6
|
15小时前
|
设计模式 算法 中间件
【C++ 可调用对象的应用】C++设计模式与现代编程技巧:深入可调用对象的世界
【C++ 可调用对象的应用】C++设计模式与现代编程技巧:深入可调用对象的世界
127 1
|
15小时前
|
设计模式 机器学习/深度学习 算法
C++设计模式新篇章:掌握状态委托
C++设计模式新篇章:掌握状态委托
75 0
|
15小时前
|
设计模式 算法 C++
从 C++ 优化状态机实现:结合设计模式的实用指南
从 C++ 优化状态机实现:结合设计模式的实用指南
71 1
|
15小时前
|
设计模式 存储 安全
C++多线程管理的艺术:从基础到设计模式
C++多线程管理的艺术:从基础到设计模式
67 0
|
14小时前
|
设计模式 API
【设计模式】适配器和桥接器模式有什么区别
【设计模式】适配器和桥接器模式有什么区别
7 1
|
14小时前
|
设计模式
【设计模式】张一鸣笔记:责任链接模式怎么用?
【设计模式】张一鸣笔记:责任链接模式怎么用?
10 1
|
14小时前
|
设计模式 uml
【设计模式】建造者模式就是游戏模式吗?
【设计模式】建造者模式就是游戏模式吗?
7 0