桥接模式(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 将两个层次桥接,帮助实现抽象和具体逻辑的解耦。
相关推荐
找了一圈尾巴7 小时前
设计模式(结构型)-桥接模式
设计模式·桥接模式
匹马夕阳7 小时前
java开发中的设计模式之单例模式
java·单例模式·设计模式
啊QQQQQ11 小时前
设计模式-原型模式
java·设计模式·原型模式
xiaowu08011 小时前
C#设计模式-状态模式
设计模式·c#·状态模式
编程侦探12 小时前
【设计模式】适配器模式:让不兼容的接口和谐共处
开发语言·c++·设计模式·适配器模式
骊山道童13 小时前
设计模式-桥接模式
设计模式·桥接模式
程序员JerrySUN13 小时前
设计模式每日硬核训练 Day 12:装饰器模式(Decorator Pattern)完整讲解与实战应用
设计模式·装饰器模式
朝花惜时13 小时前
物流网络规划-让AI用线性规划方式求解
设计模式·数据挖掘·数据可视化
听闻风很好吃14 小时前
Java设计模式之观察者模式:从入门到架构级实践
java·观察者模式·设计模式
胎粉仔15 小时前
Swift —— delegate 设计模式
开发语言·设计模式·swift