桥接模式(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 将两个层次桥接,帮助实现抽象和具体逻辑的解耦。
相关推荐
杯子不吃紫菜4 小时前
LangGraph 体系实战(下):中断恢复、流式、子图与设计模式
单片机·嵌入式硬件·设计模式
是2的10次方啊6 小时前
设计模式与生活⑳:楼里的事都找前台,你就再没见过师傅
设计模式
是2的10次方啊1 天前
设计模式与生活⑲:投影连不上,别急着换投影仪
设计模式
cjz38992 天前
规则树:把一百行if-else拆成一棵树
设计模式
Adellle2 天前
设计模式指南 (Java 版) ㊁
java·开发语言·设计模式
是2的10次方啊2 天前
设计模式与生活⑱:工资还没焐热,账单已经凑成一摞
设计模式
百思不得小李3 天前
一个复杂的动态表单是如何实现的
前端·设计模式
爱你真是太好了3 天前
上下文组成以及如何压缩
设计模式
freejackman3 天前
【工程化】从“单兵作战”到“多引擎架构”:基于门面模式的重构实战
大数据·数据库·hive·sql·测试工具·设计模式·架构
Java成神之路-3 天前
从 if-else 到优雅解耦:策略 / 适配器 / 组合模式全流程实战
设计模式