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

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

1. 装饰模式简述

1.1 目的

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

1.2 适用性

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

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

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

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

2. 装饰模式结构图

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

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

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

手机与手机软件的实现。

设计模式—桥接模式的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

原文链接: https://www.cnblogs.com/bwar/p/9191031.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/276145

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 上午1:28
下一篇 2023年2月15日 上午1:29

相关推荐