(二)结构型模式:2、桥接模式(Bridge Pattern)(C++实现示例)

目录

[1、桥接模式(Bridge Pattern)含义](#1、桥接模式(Bridge Pattern)含义)

2、桥接模式应用场景

3、桥接模式的UML图学习

4、C++实现桥接模式的示例


1、桥接模式(Bridge Pattern)含义

桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化。

2、桥接模式应用场景

1)当你希望抽象部分和实现部分可以独立地扩展和变化时,可以使用桥接模式。它可以让抽象部分和实现部分可以独立地进行演化,而不会相互影响。

2)当一个类存在多个维度的变化,且每个维度都需要独立地进行扩展时,可以考虑使用桥接模式。通过将每个维度的变化抽象为不同的继承层次结构,然后通过桥接模式将这些继承层次结构连接起来,可以灵活地组合不同的变化。

3)当需要在运行时动态地选择或切换抽象部分和实现部分的关系时,可以使用桥接模式。通过将抽象部分和实现部分解耦,并通过组合的方式建立关联,可以在运行时灵活地选择具体的实现。

4)当希望对抽象部分和实现部分进行共享和复用时,可以考虑使用桥接模式。通过将抽象部分和实现部分分离,可以使它们可以独立地进行复用,从而提高代码的可维护性和可扩展性。

总的来说,桥接模式适用于抽象部分和实现部分需要独立演化、存在多个维度的变化、需要动态选择关系或希望进行共享和复用的场景。它可以提供更好的灵活性、可扩展性和可维护性。

3、桥接模式的UML图学习

在上述UML类图中,有以下几个关键角色:

  • Abstraction(抽象类):定义了抽象部分的接口,并包含一个指向Implementor的成员变量。它的具体子类可以通过调用Implementor的方法来实现自己的操作。

  • Implementor(实现类接口):定义了实现部分的接口,它通常只提供了一些基本的操作方法。

  • ConcreteImplementorAConcreteImplementorB(具体实现类):实现了Implementor接口,具体实现了实现部分的操作方法。

  • RefinedAbstractionARefinedAbstractionB(扩展抽象类):继承自Abstraction,并通过调用Implementor的方法来实现自己的操作。

在桥接模式中,抽象部分和实现部分通过关联关系连接在一起,而不是继承关系。这样可以使得抽象部分和实现部分可以独立地变化和演化。

4、C++实现桥接模式的示例

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

// 实现接口
class Implementor 
{
public:
    virtual void operationImpl() const = 0;
};

// 具体实现类A
class ConcreteImplementorA : public Implementor 
{
public:
    void operationImpl() const override 
    {
        std::cout << "ConcreteImplementorA: operationImpl" << std::endl;
    }
};

// 具体实现类B
class ConcreteImplementorB : public Implementor 
{
public:
    void operationImpl() const override 
    {
        std::cout << "ConcreteImplementorB: operationImpl" << std::endl;
    }
};

// 抽象类
class Abstraction {
protected:
    Implementor* m_implementor;

public:
    Abstraction(Implementor* implementor) : m_implementor(implementor) {}

    virtual void operation() const = 0;
};

// 扩展抽象类A
class RefinedAbstractionA : public Abstraction 
{
public:
    RefinedAbstractionA(Implementor* implementor) : Abstraction(implementor) {}

    void operation() const override {
        std::cout << "RefinedAbstractionA: ";
        m_implementor->operationImpl();
    }
};

// 扩展抽象类B
class RefinedAbstractionB : public Abstraction 
{
public:
    RefinedAbstractionB(Implementor* implementor) : Abstraction(implementor) {}

    void operation() const override {
        std::cout << "RefinedAbstractionB: ";
        m_implementor->operationImpl();
    }
};

int main() 
{
    // 创建具体实现类对象


    Implementor* implementorA = new ConcreteImplementorA();
    Implementor* implementorB = new ConcreteImplementorB();

    // 使用扩展抽象类A调用操作


    Abstraction* abstractionA = new RefinedAbstractionA(implementorA);
    abstractionA->operation();

    // 使用扩展抽象类B调用操作


    Abstraction* abstractionB = new RefinedAbstractionB(implementorB);
    abstractionB->operation();

    delete implementorA;
    delete implementorB;
    delete abstractionA;
    delete abstractionB;

    return 0;
}

在上述示例中,我们首先定义了实现接口(Implementor),其中包含了一个纯虚函数operationImpl()。然后,我们实现了两个具体的实现类(ConcreteImplementorA和ConcreteImplementorB),它们分别继承自实现接口,并实现了接口中的纯虚函数。

接着,我们定义了抽象类(Abstraction),其中包含了一个指向实现接口的指针,并声明了一个纯虚函数operation()。然后,我们通过扩展抽象类A(RefinedAbstractionA)和扩展抽象类B(RefinedAbstractionB)来实现具体的抽象类,它们分别继承自抽象类,并实现了抽象类中的纯虚函数。

在主函数中,我们创建了具体的实现类对象(implementorA和implementorB),然后通过扩展抽象类A和扩展抽象类B来调用操作。在调用操作时,实际上会调用到具体的实现类中的具体函数。

相关推荐
yuyanjingtao3 分钟前
CCF-GESP 等级考试 2025年9月认证C++一级真题解析
c++·青少年编程·gesp·csp-j/s
xzk2012100214 分钟前
洛谷 P1438 无聊的数列 题解
c++·算法·树状数组
00后程序员张17 分钟前
C++ string 类使用攻略
开发语言·c++
OKkankan20 分钟前
list的使用和模拟实现
数据结构·c++·算法·list
keep intensify1 小时前
Redis基础指令全解析:从入门到精通
linux·数据库·c++·redis
爱吃生蚝的于勒1 小时前
【Linux】零基础学会linux环境基础开发工具使用(yum,vim,makefile,gdb)
linux·服务器·数据结构·c++·蓝桥杯·编辑器·vim
R-G-B1 小时前
【34】MFC入门到精通——MFC 控件 ComboBox 运行点击控件下拉框 “终止“、“重试“、“忽略“、“引发异常”
c++·mfc·combobox“引发异常”·“终止“·“重试“·“忽略“·“引发异常”
零基础的修炼1 小时前
Linux---线程封装
linux·c++·算法
给大佬递杯卡布奇诺1 小时前
FFmpeg 基本API avio_read函数内部调用流程分析
c++·ffmpeg·音视频
liulilittle2 小时前
Y组合子剖析:C++ 中的递归魔法
开发语言·c++·编程语言·函数式编程·函数式·函数编程·y组合子