桥接模式(Bridge Pattern)

桥接模式 (Bridge Pattern)是一种结构型设计模式,用于将抽象部分 与实现部分分离,使它们可以独立变化。它的核心思想是通过引入一个"桥"类,将抽象层与实现层解耦。


桥接模式的结构

  • Abstraction(抽象部分)

    定义高层的抽象接口,提供公共的方法声明。

  • Implementor(实现接口部分)

    定义实现部分的接口,一般是抽象类或接口。

  • ConcreteImplementor(具体实现部分)

    实现具体的逻辑,继承自 Implementor。

  • RefinedAbstraction(扩展抽象部分)

    扩展或细化抽象部分的逻辑。

桥接模式的核心是通过 "组合" 而非 "继承" 将抽象部分与实现部分关联。


桥接模式的代码示例

示例场景:不同平台上的视频播放器

我们创建一个桥接模式,处理跨平台的多种视频播放器:

  1. 抽象出一个播放器的抽象类 VideoPlayer。
  2. 提供具体的实现部分接口 Platform。
  3. 通过桥接的方式组合它们。

示例代码
复制代码

cpp

cpp 复制代码
#include <iostream>
#include <memory>
#include <string>

using namespace std;

// Implementor:定义实现接口
class Platform {
public:
    virtual ~Platform() = default;
    virtual void playVideo(const string& video) = 0;
};

// ConcreteImplementorA:Windows 实现
class WindowsPlatform : public Platform {
public:
    void playVideo(const string& video) override {
        cout << "Playing video on Windows: " << video << endl;
    }
};

// ConcreteImplementorB:Linux 实现
class LinuxPlatform : public Platform {
public:
    void playVideo(const string& video) override {
        cout << "Playing video on Linux: " << video << endl;
    }
};

// Abstraction:播放器抽象接口
class VideoPlayer {
protected:
    shared_ptr<Platform> platform; // 桥接到具体实现的接口

public:
    explicit VideoPlayer(shared_ptr<Platform> p) : platform(std::move(p)) {}
    virtual ~VideoPlayer() = default;
    virtual void play(const string& video) = 0;
};

// RefinedAbstraction:高级播放器
class AdvancedPlayer : public VideoPlayer {
public:
    explicit AdvancedPlayer(shared_ptr<Platform> p) : VideoPlayer(std::move(p)) {}

    void play(const string& video) override {
        cout << "AdvancedPlayer: Preparing video..." << endl;
        platform->playVideo(video); // 调用桥接的具体实现
        cout << "AdvancedPlayer: Finished playing video." << endl;
    }
};

// RefinedAbstraction:普通播放器
class SimplePlayer : public VideoPlayer {
public:
    explicit SimplePlayer(shared_ptr<Platform> p) : VideoPlayer(std::move(p)) {}

    void play(const string& video) override {
        cout << "SimplePlayer: ";
        platform->playVideo(video); // 调用桥接的具体实现
    }
};

// 客户端代码
int main() {
    // 创建具体实现:Windows 平台
    shared_ptr<Platform> windows = make_shared<WindowsPlatform>();
    // 创建具体实现:Linux 平台
    shared_ptr<Platform> linux = make_shared<LinuxPlatform>();

    // 使用不同实现创建播放器
    VideoPlayer* player1 = new AdvancedPlayer(windows);
    VideoPlayer* player2 = new SimplePlayer(linux);

    // 播放视频
    player1->play("movie.mp4");
    player2->play("song.mp4");

    // 释放资源
    delete player1;
    delete player2;

    return 0;
}

输出结果
复制代码
cpp 复制代码
AdvancedPlayer: Preparing video...
Playing video on Windows: movie.mp4
AdvancedPlayer: Finished playing video.
SimplePlayer: Playing video on Linux: song.mp4

桥接模式的分析

  1. 角色分离

    • 抽象层 :VideoPlayer 提供一个统一的高层接口。
    • 实现层 :Platform 定义跨平台的实现接口。
  2. 灵活性

    • 平台层(Windows/Linux)和播放器层(Simple/Advanced)可以独立扩展,相互解耦。
  3. 桥接点

    • VideoPlayer 和 Platform 通过组合关联而不是继承。

桥接模式与 Android 图形栈的关系

在 Android 图形栈中,类似的桥接模式如下:

  • 抽象层:高层使用的 IGraphicBufferProducer。
  • 实现层:具体硬件或系统服务提供的 HAL 实现。
  • 桥接点:B2HGraphicBufferProducer 将两个层次桥接,帮助实现抽象和具体逻辑的解耦。
相关推荐
阿狗童鞋15 小时前
设计模式实战指南
设计模式
电子科技圈21 小时前
基于车载SerDes和OpenGMSL协议的SoC设计与验证:行业高频问题解析
设计模式·车载系统·软件构建·设计规范
ClinicTech1 天前
高速冷冻离心机的温控系统与安全保护逻辑解析
设计模式·架构·健康医疗·设计语言
GISMagic1 天前
3.《设计模式》:软件设计与解耦能力提炼
设计模式·ai coding
染指11101 天前
131.Agent-Agent设计模式-MAS多智能体系统(Multi-Agent-System)
人工智能·设计模式·langchain·agent·agents
ShyanZh2 天前
【Python3基础】13-Python 常用设计模式
开发语言·python·设计模式
YYYing.2 天前
【设计模式系列 (五) 】原型模式
开发语言·后端·设计模式·原型模式·c/c++
长按助力退休3 天前
设计模式实战,这 5 个最常用
设计模式
Carl_奕然3 天前
【智能体】Agent的四种设计模式之:Multi-Agent Collaboration
设计模式
Lost of 程序猿3 天前
建造者模式实战:告别“十参数构造函数“的数据导出任务
后端·设计模式·c#·asp.net