桥接模式(大话设计模式)C/C++版本

桥接模式

C++

cpp 复制代码
#include <iostream>
using namespace std;

class HandsetSoft // 手机软件
{
public:
    virtual ~HandsetSoft() = default;
    virtual void Run() = 0;
};

class HandsetGame : public HandsetSoft // 手机游戏
{
public:
    void Run() override
    {
        cout << "运行手机游戏!" << endl;
    }
};

class HandsetAddressList : public HandsetSoft // 手机通讯录
{
public:
    void Run() override
    {
        cout << "运行手机通讯录!" << endl;
    }
};

class HandsetBrand // 手机品牌类
{
protected:
    HandsetSoft *m_soft; // 手机品牌中包含着手机通讯录和手机游戏
public:
    virtual ~HandsetBrand() = default;
    void SetHandsetSoft(HandsetSoft *soft)
    {
        m_soft = soft;
    }
    virtual void Run() = 0;
};

class HandsetBrandN : public HandsetBrand // 手机品牌 N
{
public:
    void Run() override
    {
        m_soft->Run();
    }
};

class HandsetBrandM : public HandsetBrand // 手机品牌M
{
public:
    void Run() override
    {
        m_soft->Run();
    }
};

int main()
{
    cout << "手机品牌N:" << endl;
    HandsetBrand *abn = new HandsetBrandN();
    HandsetGame *hgn = new HandsetGame();
    abn->SetHandsetSoft(hgn);
    abn->Run();

    HandsetAddressList *haln = new HandsetAddressList();

    abn->SetHandsetSoft(haln);
    abn->Run();

    cout << endl
         << "手机品牌M:" << endl;
    HandsetBrand *abm = new HandsetBrandM();
    HandsetGame *hgm = new HandsetGame();
    abm->SetHandsetSoft(hgm);
    abm->Run();

    HandsetAddressList *halm = new HandsetAddressList();
    abm->SetHandsetSoft(halm);
    abm->Run();
    delete abn;
    delete hgn;
    delete haln;
    delete abm;
    delete hgm;
    delete halm;
    return 0;
}

C

c 复制代码
#include <stdio.h>

// 手机软件抽象基类
typedef struct HandsetSoft
{
    void (*run)(struct HandsetSoft *self); // 虚函数表
} HandsetSoft;

// 手机游戏类
typedef struct HandsetGame
{
    HandsetSoft base;
} HandsetGame;

void run_game(HandsetSoft *self)
{
    printf("运行手机游戏!\n");
}

// 手机通讯录类
typedef struct HandsetAddressList
{
    HandsetSoft base;
} HandsetAddressList;

void run_address_list(HandsetSoft *self)
{
    printf("运行手机通讯录!\n");
}

// 手机品牌基类
typedef struct HandsetBrand
{
    HandsetSoft *m_soft;
} HandsetBrand;

void set_handset_soft(HandsetBrand *self, HandsetSoft *soft)
{
    self->m_soft = soft;
}

// 手机品牌N类
typedef struct HandsetBrandN
{
    HandsetBrand base;
} HandsetBrandN;

void run_brandN(HandsetBrand *self)
{
    self->m_soft->run(self->m_soft);
}

// 手机品牌M类
typedef struct HandsetM
{
    HandsetBrand base;
} HandsetM;

void run_brandM(HandsetBrand *self)
{
    self->m_soft->run(self->m_soft);
}

int main()
{
    // 初始化手机软件
    HandsetSoft game_software = {.run = run_game};
    HandsetSoft address_list_software = {.run = run_address_list};
    HandsetGame game = {.base = game_software};
    HandsetAddressList address_list = {.base = address_list_software};

    // 初始化手机品牌N
    HandsetBrandN brandN = {};
    set_handset_soft((HandsetBrand *)&brandN, (HandsetSoft *)&game);
    printf("手机品牌N:\n");
    run_brandN((HandsetBrand *)&brandN);
    set_handset_soft((HandsetBrand *)&brandN, (HandsetSoft *)&address_list);
    run_brandN((HandsetBrand *)&brandN);

    // 初始化手机品牌M
    HandsetM brandM = {};
    set_handset_soft((HandsetBrand *)&brandM, (HandsetSoft *)&game);
    printf("\n手机品牌M:\n");
    run_brandM((HandsetBrand *)&brandM);
    set_handset_soft((HandsetBrand *)&brandM, (HandsetSoft *)&address_list);
    run_brandM((HandsetBrand *)&brandM);

    return 0;
}
相关推荐
玩转C语言和数据结构4 分钟前
C语言编程入门攻略(最新学习路线,适合新手小白)
c语言·c语言入门·c语言下载·c语言知识点总结·c语言自学·c语言教程·c语言怎么学
y***548834 分钟前
PHP框架设计模式
设计模式
Bona Sun42 分钟前
单片机手搓掌上游戏机(十四)—pico运行fc模拟器之电路连接
c语言·c++·单片机·游戏机
无限进步_2 小时前
C语言数组元素删除算法详解:从基础实现到性能优化
c语言·开发语言·windows·git·算法·github·visual studio
松涛和鸣2 小时前
16、C 语言高级指针与结构体
linux·c语言·开发语言·数据结构·git·算法
口袋物联3 小时前
设计模式之适配器模式在 C 语言中的应用(含 Linux 内核实例)
c语言·设计模式·适配器模式
MobotStone3 小时前
大数据:我们是否在犯一个大错误?
设计模式·架构
!停4 小时前
函数递归的应用
c语言
7***n755 小时前
前端设计模式详解
前端·设计模式·状态模式
兵bing5 小时前
设计模式-装饰器模式
设计模式·装饰器模式