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

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

cpp 复制代码
#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个类
相关推荐
花好月圆春祺夏安37 分钟前
基于odoo17的设计模式详解---备忘模式
数据库·设计模式
真的想上岸啊39 分钟前
学习C++、QT---21(QT中QFile库的QFile读取文件、写入文件的讲解)
c++·qt·学习
小庞在加油1 小时前
Apollo源码架构解析---附C++代码设计示例
开发语言·c++·架构·自动驾驶·apollo
我喜欢就喜欢2 小时前
RapidFuzz-CPP:高效字符串相似度计算的C++利器
开发语言·c++
千帐灯无此声2 小时前
Linux 测开:日志分析 + 定位 Bug
linux·c语言·c++·bug
莫彩2 小时前
【Modern C++ Part7】_创建对象时使用()和{}的区别
开发语言·c++
mit6.8244 小时前
[Meetily后端框架] Whisper转录服务器 | 后端服务管理脚本
c++·人工智能·后端·python
DKPT5 小时前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
L_autinue_Star6 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
无小道8 小时前
c++--typedef和#define的用法及区别
c语言·开发语言·汇编·c++