长生栈 长生栈
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Living Team

编程技术分享
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 创建型模式

  • 结构型模式

    • 适配器模式(Adapter Pattern)
    • 桥接模式(Bridge Pattern)
      • 概念
      • 思想
      • 角色
      • 优点
      • 缺点
      • 类图
      • 示例代码
    • 组合模式(Composite Pattern)
    • 装饰器模式(Decorator Pattern)
    • 外观模式(Facade Pattern)
    • 享元模式(Flyweight Pattern)
    • 代理模式(Proxy Pattern)
  • 行为型模式

  • 设计模式
  • 结构型模式
DC Wang
2023-05-28
目录

桥接模式(Bridge Pattern)

# 桥接模式(Bridge Pattern)

# 概念

桥接模式是一种结构型设计模式,它将抽象部分与其具体实现部分分离,使它们可以独立变化。通过桥接模式,可以将一个类的抽象和实现层级结构分开,使它们可以独立扩展。

# 思想

桥接模式的核心思想是将抽象和实现解耦,让它们可以独立地变化。抽象部分定义了高层接口,实现部分定义了底层实现。通过桥接模式,可以在运行时选择不同的实现,从而实现更灵活的功能扩展。

# 角色

  • Abstraction(抽象类):定义抽象接口,维护一个指向实现类的引用。
  • RefinedAbstraction(扩充抽象类):对抽象类进行扩展,实现更多功能。
  • Implementor(实现类接口):定义实现类的接口。
  • ConcreteImplementor(具体实现类):实现Implementor接口的具体类。

# 优点

  • 可以将抽象部分和实现部分独立扩展,彼此之间不会影响。
  • 提高了系统的可扩展性,符合开闭原则。
  • 提供了一种动态切换实现的方式,增加了灵活性。

# 缺点

  • 增加了系统的复杂性,需要额外的抽象和实现类。
  • 对于简单的应用场景,桥接模式可能显得过于繁琐。

# 类图

以下是桥接模式的类图,使用PlantUML绘制:

@startuml

class Abstraction {
  +operation()
}

class RefinedAbstraction {
  +operation()
}

interface Implementor {
  +operationImpl()
}

class ConcreteImplementorA {
  +operationImpl()
}

class ConcreteImplementorB {
  +operationImpl()
}

Abstraction --> Implementor
RefinedAbstraction --> Abstraction
RefinedAbstraction --> Implementor
ConcreteImplementorA --|> Implementor
ConcreteImplementorB --|> Implementor

note right of Abstraction: 包含对实现类的引用
note left of Implementor: 定义实现类的接口

@enduml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 示例代码

下面是一个使用C++实现的桥接模式示例代码:

#include <iostream>

// Implementor
class Implementor {
public:
    virtual void operationImpl() = 0;
};

// ConcreteImplementorA
class ConcreteImplementorA : public Implementor {
public:
    void operationImpl() override {
        std::cout << "ConcreteImplementorA operationImpl() called" << std::endl;
    }
};

// ConcreteImplementorB
class ConcreteImplementorB : public Implementor {
public:
    void operationImpl() override {
        std::cout << "ConcreteImplementorB operationImpl() called" << std::endl;
    }
};

// Abstraction
class Abstraction {
protected:
    Implementor* implementor;

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

    virtual void operation() {
        implementor->operationImpl();
    }
};

// RefinedAbstraction
class RefinedAbstraction : public Abstraction {
public:
    RefinedAbstraction(Implementor* impl) : Abstraction(impl) {}

    void operation() override {
        std::cout << "RefinedAbstraction operation() called" << std::endl;
        implementor->operationImpl();
    }
};

int main() {
    Implementor* implA = new ConcreteImplementorA();
    Implementor* implB = new ConcreteImplementorB();

    Abstraction* abstractionA = new RefinedAbstraction(implA);
    abstractionA->operation();

    Abstraction* abstractionB = new RefinedAbstraction(implB);
    abstractionB->operation();

    delete implA;
    delete implB;
    delete abstractionA;
    delete abstractionB;

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

代码运行结果:

RefinedAbstraction operation() called
ConcreteImplementorA operationImpl() called
RefinedAbstraction operation() called
ConcreteImplementorB operationImpl() called
1
2
3
4

示例代码中,我们定义了两个具体实现类 ConcreteImplementorA 和 ConcreteImplementorB,它们都实现了 Implementor 接口。抽象类 Abstraction 包含对实现类的引用,并定义了一个抽象方法 operation。扩充抽象类 RefinedAbstraction 对抽象类进行了扩展,实现了更多的功能。

在示例代码中,我们首先创建了 ConcreteImplementorA 的实例,并将其传递给 RefinedAbstraction 的构造函数创建了 abstractionA 对象。然后调用 abstractionA 的 operation 方法,它会调用 ConcreteImplementorA 的 operationImpl 方法。

接着,我们创建了 ConcreteImplementorB 的实例,并将其传递给 RefinedAbstraction 的构造函数创建了 abstractionB 对象。同样地,调用 abstractionB 的 operation 方法,它会调用 ConcreteImplementorB 的 operationImpl 方法。

代码的运行结果显示了各个类的方法被调用的顺序和输出结果,验证了桥接模式的实现。

编辑 (opens new window)
#设计模式#结构型模式
上次更新: 2023/06/09, 13:17:31
适配器模式(Adapter Pattern)
组合模式(Composite Pattern)

← 适配器模式(Adapter Pattern) 组合模式(Composite Pattern)→

最近更新
01
ESP32-网络摄像头方案
06-14
02
ESP32-PWM驱动SG90舵机
06-14
03
ESP32-实时操作系统freertos
06-14
更多文章>
Theme by Vdoing | Copyright © 2019-2025 DC Wang All right reserved | 辽公网安备 21021102001125号 | 吉ICP备20001966号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式