桥接模式(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. 桥接点

    • VideoPlayerPlatform 通过组合关联而不是继承。

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

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

  • 抽象层:高层使用的 IGraphicBufferProducer
  • 实现层:具体硬件或系统服务提供的 HAL 实现。
  • 桥接点:B2HGraphicBufferProducer 将两个层次桥接,帮助实现抽象和具体逻辑的解耦。
相关推荐
葬送的代码人生5 小时前
别再让 AI 瞎写代码了!Vibe Coding 三步法教你写出靠谱代码
前端·设计模式·架构
无风听海8 小时前
Claude Agent Skills 的四种设计模式;从渐进式披露到最小权限
java·算法·设计模式
富贵冼中求9 小时前
从单向流到双向 RPC:Agent 通信协议的范式分叉与 ACP 协议实战拆解
设计模式·架构
电子科技圈13 小时前
先进封装、芯粒架构和3D集成——先进异构集成亟需兼具标准化与定制化能力的互联及总线IP解决方案
tcp/ip·设计模式·架构·软件构建·代码规范·设计规范
zjun100114 小时前
C++:2.工厂模式
设计模式
触底反弹15 小时前
🤯 面试被问 AI Workflow 和 Agent 有啥区别?3 张图 + 2 段代码讲清楚!
人工智能·设计模式·面试
杨充1 天前
10.可测试性实战设计
设计模式·开源·代码规范
杨充1 天前
9.重构十二式的实战
设计模式·开源·代码规范
杨充2 天前
6.设计原则的全景图
设计模式·开源·全栈
杨充2 天前
2.面向对象的特性
设计模式