C++ 设计模式:桥接模式(Bridge Pattern)

链接:C++ 设计模式
链接:C++ 设计模式 - 装饰模式

桥接模式(Bridge Pattern)是一种结构型设计模式,它通过将抽象部分(业务功能)与实现部分(平台实现)分离,使它们可以独立变化。桥接模式的核心思想是将抽象与实现解耦,使得它们可以独立地扩展和变化。

1.问题分析

在开发中,我们经常遇到需要将抽象部分与其实现部分分离的情况,以便它们可以独立变化。例如:

  • 不同的任务(如巡逻、运输、侦查)可以使用不同的设备(如机器人、传感器)。
  • 不同的图形形状(如圆形、矩形)可以使用不同的绘制方式(如矢量图、位图)。

如果将抽象部分与实现部分紧密耦合在一起,当需要添加新的实现或新的抽象时,代码的修改和维护将变得非常复杂。桥接模式通过将抽象部分与实现部分分离,使它们可以独立变化,从而提高系统的灵活性和可扩展性。

2.实现步骤

  1. 定义实现接口:定义一个接口,描述实现部分的行为。
  2. 实现具体实现:实现具体实现类,提供不同的实现部分。
  3. 定义抽象类:定义一个抽象类,描述抽象部分的行为,并持有一个实现部分的引用。
  4. 实现具体抽象:实现具体抽象类,提供不同的抽象部分。
  5. 客户端代码:动态地组合不同的抽象部分和实现部分,并执行任务。

3.代码示例

以机器人的任务作为示例。

3.1.定义实现接口

cpp 复制代码
// 机器人平台接口
class RobotPlatform {
 public:
  virtual ~RobotPlatform() = default;
  virtual void move() = 0;
};

3.2. 实现具体实现类

cpp 复制代码
// 具体机器人平台1:四足机器人
class QuadrupedRobot : public RobotPlatform {
 public:
  void move() override { std::cout << "Moving as a quadruped robot." << std::endl; }
};
cpp 复制代码
// 具体机器人平台2:轮式机器人
class WheeledRobot : public RobotPlatform {
 public:
  void move() override { std::cout << "Moving as a wheeled robot." << std::endl; }
};
cpp 复制代码
// 具体机器人平台3:履带机器人
class TrackedRobot : public RobotPlatform {
 public:
  void move() override { std::cout << "Moving as a tracked robot." << std::endl; }
};

3.3. 定义任务抽象类

cpp 复制代码
// 定义一个任务抽象类,描述机器人的任务行为,并持有一个机器人平台对象的引用
class Task {
 public:
  Task(std::shared_ptr<RobotPlatform> robotPlatform) : platform(robotPlatform) {}

  virtual void performTask() = 0;

 protected:
  std::shared_ptr<RobotPlatform> platform;
};

3.4. 实现具体的任务

cpp 复制代码
// 具体任务1:巡逻
class Patrolling : public Task {
 public:
  Patrolling(std::shared_ptr<RobotPlatform> robotPlatform) : Task(robotPlatform) {}

  void performTask() override {
    std::cout << "Performing patrolling task." << std::endl;
    platform->move();
  }
};
cpp 复制代码
// 具体任务2:运输
class Transporting : public Task {
 public:
  Transporting(std::shared_ptr<RobotPlatform> robotPlatform) : Task(robotPlatform) {}

  void performTask() override {
    std::cout << "Performing transporting task." << std::endl;
    platform->move();
  }
};
cpp 复制代码
// 具体任务3:侦查
class Reconnaissance : public Task {
 public:
  Reconnaissance(std::shared_ptr<RobotPlatform> robotPlatform) : Task(robotPlatform) {}

  void performTask() override {
    std::cout << "Performing reconnaissance task." << std::endl;
    platform->move();
  }
};

3.5. 客户端代码

cpp 复制代码
// 动态地组合不同的任务和机器人平台,并执行任务。
int main() {
  // 创建具体的机器人平台
  std::shared_ptr<RobotPlatform> quadrupedRobot = std::make_shared<QuadrupedRobot>();
  std::shared_ptr<RobotPlatform> wheeledRobot = std::make_shared<WheeledRobot>();
  std::shared_ptr<RobotPlatform> trackedRobot = std::make_shared<TrackedRobot>();

  // 创建具体的任务,并组合合理的机器人平台
  std::shared_ptr<Task> patrollingWithQuadruped = std::make_shared<Patrolling>(quadrupedRobot);
  std::shared_ptr<Task> transportingWithWheeled = std::make_shared<Transporting>(wheeledRobot);
  std::shared_ptr<Task> reconnaissanceWithTracked = std::make_shared<Reconnaissance>(trackedRobot);

  // 执行任务
  std::cout << "Patrolling with Quadruped Robot:" << std::endl;
  patrollingWithQuadruped->performTask();

  std::cout << "\nTransporting with Wheeled Robot:" << std::endl;
  transportingWithWheeled->performTask();

  std::cout << "\nReconnaissance with Tracked Robot:" << std::endl;
  reconnaissanceWithTracked->performTask();

  return 0;
}

4.总结

桥接模式和装饰模式的区别:

  • 桥接模式:将抽象部分与实现部分分离,使它们可以独立变化。它通过将抽象和实现放在不同的类层次结构中,并通过组合将它们连接起来,从而提高系统的灵活性和可扩展性。。
  • 装饰模式:动态地给对象添加职责,而不改变对象的接口。它通过创建一系列装饰器类来实现这些职责的扩展,从而在运行时灵活地扩展对象的功能。
相关推荐
LXS_35720 小时前
Day 18 C++提高 之 STL常用容器(string、vector、deque)
开发语言·c++·笔记·学习方法·改行学it
deng-c-f21 小时前
Linux C/C++ 学习日记(53):原子操作(二):实现shared_ptr
开发语言·c++·学习
一个不知名程序员www21 小时前
算法学习入门---结构体和类(C++)
c++·算法
墨雪不会编程1 天前
C++ string 详解:STL 字符串容器的使用技巧
java·开发语言·c++
yangpipi-1 天前
《C++并发编程实战》第5章 C++内存模型和原子操作
android·java·c++
SunkingYang1 天前
MFC进程间消息通信深度解析:SendMessage、PostMessage与SendNotifyMessage的底层实现与实战指南
c++·mfc·共享内存·通信·postmessage·sendmessage·进程间
XFF不秃头1 天前
力扣刷题笔记-旋转图像
c++·笔记·算法·leetcode
王老师青少年编程1 天前
csp信奥赛C++标准模板库STL案例应用3
c++·算法·stl·csp·信奥赛·lower_bound·标准模版库
Tim_101 天前
【C++入门】04、C++浮点型
开发语言·c++
hkNaruto1 天前
【C++】记录一次C++程序编译缓慢原因分析——滥用stdafx.h公共头文件
开发语言·c++