桥接模式(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 将两个层次桥接,帮助实现抽象和具体逻辑的解耦。
相关推荐
勤奋的知更鸟9 天前
Java 编程之责任链模式
java·开发语言·设计模式·责任链模式
逆袭的菜鸟X9 天前
JS常用设计模式汇总
开发语言·javascript·设计模式
uplinker10 天前
设计模式-三大工厂
java·开发语言·设计模式
像污秽一样10 天前
软件设计模式期末复习模拟解析
java·设计模式·软件设计模式·复习·java设计模式
SoFlu软件机器人10 天前
AI 领航设计模式学习:飞算 JavaAI 解锁单例模式实践新路径
学习·单例模式·设计模式
尤物程序猿10 天前
设计模式之手写策略模式实现动态支付(Java实现)
java·设计模式·策略模式
极地星光10 天前
设计模式(C++/Qt)-工厂模式
c++·qt·设计模式
勤奋的知更鸟10 天前
Java 编程之命令模式
java·开发语言·设计模式·命令模式
charlie11451419110 天前
从C++编程入手设计模式——命令模式
c++·设计模式·命令模式
剁椒豆腐脑10 天前
阶段二JavaSE进阶阶段之设计模式&继承 2.2
java·设计模式·跳槽·学习方法·改行学it