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

桥接模式

在这里插入图片描述

C++

#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

#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;
}

相关推荐

  1. 设计模式

    2024-07-09 17:04:12       27 阅读
  2. 设计模式模式

    2024-07-09 17:04:12       37 阅读
  3. 设计模式——模式

    2024-07-09 17:04:12       37 阅读
  4. 设计模式-模式

    2024-07-09 17:04:12       35 阅读

最近更新

  1. 揭秘Odoo OWL的魔法:reactive vs useState

    2024-07-09 17:04:12       0 阅读
  2. PS像素图层简介

    2024-07-09 17:04:12       0 阅读

热门阅读

  1. 细水长流:SKlearn中模型的增量训练实践

    2024-07-09 17:04:12       7 阅读
  2. 论如何搭建属于自己的服务器?

    2024-07-09 17:04:12       6 阅读
  3. RabbitMQ实现延迟消息

    2024-07-09 17:04:12       10 阅读
  4. 【数据挖掘】银行信用卡风险大数据分析与挖掘

    2024-07-09 17:04:12       7 阅读
  5. emacs 重新加载磁盘上的文件

    2024-07-09 17:04:12       7 阅读
  6. Linux学习笔记(一)

    2024-07-09 17:04:12       8 阅读
  7. 用WPF实现的窗体是怎么运行的?

    2024-07-09 17:04:12       9 阅读
  8. Mac OS M3 安装 Docker 并解决芯片不支持问题

    2024-07-09 17:04:12       6 阅读
  9. Fedora 41 移除 Python 2支持

    2024-07-09 17:04:12       8 阅读