当前位置: 首页 > article >正文

C++ 设计模式-桥接模式

C++桥接模式的经典示例,包含测试代码:

#include <iostream>
#include <string>

// 实现化接口
class Device {
public:
    virtual ~Device() = default;
    virtual bool isEnabled() const = 0;
    virtual void enable() = 0;
    virtual void disable() = 0;
    virtual void setVolume(int volume) = 0;
    virtual void setChannel(int channel) = 0;
    virtual int getVolume() const = 0;
    virtual int getChannel() const = 0;
    virtual std::string getName() const = 0;
};

// 具体实现化类:电视机
class TV : public Device {
private:
    bool enabled_ = false;
    int volume_ = 0;
    int channel_ = 1;

public:
    std::string getName() const override { return "TV"; }
    bool isEnabled() const override { return enabled_; }
    void enable() override { enabled_ = true; }
    void disable() override { enabled_ = false; }
    void setVolume(int volume) override { volume_ = volume; }
    void setChannel(int channel) override { channel_ = channel; }
    int getVolume() const override { return volume_; }
    int getChannel() const override { return channel_; }
};

// 具体实现化类:收音机
class Radio : public Device {
private:
    bool enabled_ = false;
    int volume_ = 0;
    int channel_ = 100;

public:
    std::string getName() const override { return "Radio"; }
    bool isEnabled() const override { return enabled_; }
    void enable() override { enabled_ = true; }
    void disable() override { enabled_ = false; }
    void setVolume(int volume) override { volume_ = volume; }
    void setChannel(int channel) override { channel_ = channel; }
    int getVolume() const override { return volume_; }
    int getChannel() const override { return channel_; }
};

// 抽象化类:遥控器
class RemoteControl {
protected:
    Device* device_;
public:
    RemoteControl(Device* device) : device_(device) {}
    virtual ~RemoteControl() = default;

    void togglePower() {
        if (device_->isEnabled()) {
            device_->disable();
        } else {
            device_->enable();
        }
    }

    void volumeUp() {
        device_->setVolume(device_->getVolume() + 10);
    }

    void volumeDown() {
        device_->setVolume(device_->getVolume() - 10);
    }

    void channelUp() {
        device_->setChannel(device_->getChannel() + 1);
    }

    void channelDown() {
        device_->setChannel(device_->getChannel() - 1);
    }

    virtual void printStatus() const {
        std::cout << device_->getName() << " status: "
                  << (device_->isEnabled() ? "On" : "Off")
                  << ", Volume: " << device_->getVolume()
                  << ", Channel: " << device_->getChannel() << "\n";
    }
};

// 扩展抽象化类:高级遥控器
class AdvancedRemoteControl : public RemoteControl {
public:
    AdvancedRemoteControl(Device* device) : RemoteControl(device) {}

    void mute() {
        device_->setVolume(0);
    }

    void printStatus() const override {
        RemoteControl::printStatus();
        std::cout << "[Advanced features available]\n";
    }
};

// 测试代码
int main() {
    TV tv;
    Radio radio;

    // 基本遥控器控制电视
    RemoteControl basicRemote(&tv);
    std::cout << "=== Testing Basic Remote with TV ===\n";
    basicRemote.printStatus();
    
    basicRemote.togglePower();
    basicRemote.volumeUp();
    basicRemote.volumeUp();
    basicRemote.channelUp();
    basicRemote.printStatus();

    // 高级遥控器控制收音机
    AdvancedRemoteControl advancedRemote(&radio);
    std::cout << "\n=== Testing Advanced Remote with Radio ===\n";
    advancedRemote.printStatus();
    
    advancedRemote.togglePower();
    advancedRemote.mute();
    advancedRemote.channelUp();
    advancedRemote.channelUp();
    advancedRemote.printStatus();

    // 展示独立变化特性:使用高级遥控器控制电视
    AdvancedRemoteControl advancedTVRemote(&tv);
    std::cout << "\n=== Testing Advanced Remote with TV ===\n";
    advancedTVRemote.togglePower();
    advancedTVRemote.mute();
    advancedTVRemote.channelUp();
    advancedTVRemote.printStatus();

    return 0;
}

代码说明:

  1. 实现化接口(Device):定义设备的基本操作,包括电源管理、音量和频道控制。
  2. 具体实现化类(TV/Radio):实现Device接口,分别代表不同的设备类型。
  3. 抽象化类(RemoteControl)
    • 包含一个Device指针,通过组合方式桥接实现层
    • 提供基本遥控功能(开关、音量/频道调节)
  4. 扩展抽象化类(AdvancedRemoteControl)
    • 继承基础遥控器功能
    • 添加高级功能(静音)
    • 演示抽象层的扩展不影响实现层

测试输出:

=== Testing Basic Remote with TV ===
TV status: Off, Volume: 0, Channel: 1
TV status: On, Volume: 20, Channel: 2

=== Testing Advanced Remote with Radio ===
Radio status: Off, Volume: 0, Channel: 100
[Advanced features available]
Radio status: On, Volume: 0, Channel: 102
[Advanced features available]

=== Testing Advanced Remote with TV ===
TV status: On, Volume: 0, Channel: 3
[Advanced features available]

模式优势:

  1. 解耦抽象和实现:遥控器与设备独立变化,新增设备类型或遥控功能无需修改对方代码
  2. 可扩展性强:可以单独添加新的遥控器类型(如游戏手柄)或新设备类型(如投影仪)
  3. 避免类爆炸:M个遥控器×N个设备的组合只需M+N个类,而非M×N个类

http://www.kler.cn/a/543879.html

相关文章:

  • java项目之基于SSM会议管理系统的设计与实现源码(ssm+mysql)
  • nexus部署及配置https访问
  • 反弹shell
  • 保研考研机试攻略:python笔记(4)
  • soular基础教程-使用指南
  • Linux源码包安装MySQL数据库
  • 解决珠玑妙算游戏问题:C 语言实现
  • 云原生AI Agent应用安全防护方案最佳实践(上)
  • 数据库高安全—数据保护:数据动态脱敏
  • 【Stable Diffusion部署至Google Colab】
  • 使用Python爬虫获取1688公司档案信息:深入解析
  • halcon三维点云数据处理(十三)reduce_object_model_3d_by_view
  • 适配器模式 + 外观模式联合使用:新旧系统的平滑整合之道
  • visual studio 2008的试用版评估期已结束的解决办法
  • vue基础(八)
  • arkTS基础
  • 人工智能:所有144本SCI期刊都在这里(20本Top,4本On Hold)
  • JavaScript设计模式 -- 适配器模式
  • 《手札·行业篇》开源Odoo MES系统与SKF Observer Phoenix API双向对接方案
  • Notepad++ 中删除所有以 “pdf“ 结尾的行
  • CSS 核心技术知识点详解:从基础到进阶
  • 应用层优秀的共享民宿物联网框架该怎么选?
  • Kotlin 2.1.0 入门教程(十四)类、构造函数、对象、抽象类
  • mysql BUG 导致 show processlist 有大量的show slave stauts 处于init状态
  • Java调用C++动态库、入参为对象
  • websocketpp库使用:快速搭建一个websocket服务端