C++ 设计模式:中介者模式(Mediator Pattern)

链接:C++ 设计模式
链接:C++ 设计模式 - 门面模式
链接:C++ 设计模式 - 代理模式
链接:C++ 设计模式 - 适配器

中介者模式(Mediator Pattern)是行为型设计模式之一,它的主要目的是通过一个中介者对象来封装(封装变化)一系列对象之间的交互。中介者模式使各对象不需要显式地相互引用(编译时依赖-->运行时依赖),从而使其耦合松散(管理变化),而且可以独立地改变它们之间的交互。

1.问题分析

在复杂系统中,对象之间的交互可能非常复杂,导致系统难以维护和扩展。中介者模式通过引入一个中介者对象,来管理和协调这些对象之间的交互,从而简化对象之间的关系。

2.实现步骤

  1. 定义中介者接口:声明用于与各对象交互的方法。
  2. 定义参与者基类:声明交互方法。
  3. 定义具体的参与者类:创建多个具体的参与者类,实现交互方法。
  4. 定义具体中介者:实现中介者接口,并协调各对象之间的交互。
  5. 客户端代码:通过中介者与各参与者交互。

3.代码示例

以机器人群之间的通信作为示例。

3.1.定义中介者接口

cpp 复制代码
// 中介者接口
class Mediator {
 public:
  virtual void sendMessage(const std::string& message, class Robot* robot) = 0;
};

3.2.定义机器人基类

cpp 复制代码
class Robot {
 public:
  Robot(std::shared_ptr<Mediator> mediator, const std::string& name) : mediator_(mediator), name_(name) {}

  virtual void sendMessage(const std::string& message) = 0;
  virtual void receiveMessage(const std::string& message) = 0;
  std::string getName() const { return name_; }

 protected:
  std::shared_ptr<Mediator> mediator_;
  std::string name_;
};

3.3.定义具体的机器人类

cpp 复制代码
// 具体的机器人类1
class ConcreteRobot1 : public Robot {
 public:
  ConcreteRobot1(std::shared_ptr<Mediator> mediator, const std::string& name) : Robot(mediator, name) {}

  void sendMessage(const std::string& message) override { mediator_->sendMessage(message, this); }
  void receiveMessage(const std::string& message) override { std::cout << "Robot1 (" << name_ << ") received: " << message << std::endl; }
};
cpp 复制代码
// 具体的机器人类2
class ConcreteRobot2 : public Robot {
 public:
  ConcreteRobot2(std::shared_ptr<Mediator> mediator, const std::string& name) : Robot(mediator, name) {}

  void sendMessage(const std::string& message) override { mediator_->sendMessage(message, this); }
  void receiveMessage(const std::string& message) override { std::cout << "Robot2 (" << name_ << ") received: " << message << std::endl; }
};

3.4.定义具体的中介者类

cpp 复制代码
// 具体的中介者类
class ConcreteMediator : public Mediator {
 public:
  void addRobot(std::shared_ptr<Robot> robot) {
    auto it = std::find(robots_.begin(), robots_.end(), robot);
    if (it == robots_.end()) {
      robots_.push_back(robot);
    } else {
      std::cout << "Robot (" << robot->getName() << ") is already added." << std::endl;
    }
  }

  void sendMessage(const std::string& message, Robot* sender) override {
    for (const auto& robot : robots_) {
      if (robot.get() != sender) {
        robot->receiveMessage(message);
      }
    }
  }

 private:
  std::vector<std::shared_ptr<Robot>> robots_;
};

3.5.客户端代码

cpp 复制代码
int main() {
  auto mediator = std::make_shared<ConcreteMediator>();
  auto robot1 = std::make_shared<ConcreteRobot1>(mediator, "Robot1");
  auto robot2 = std::make_shared<ConcreteRobot2>(mediator, "Robot2");

  mediator->addRobot(robot1);
  mediator->addRobot(robot2);

  robot1->sendMessage("My position is (10, 20)");
  robot2->sendMessage("My position is (30, 40)");

  return 0;
}
相关推荐
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
weilx12341 天前
C++笔记-文件IO-<fcntl.h>
c++
Smileyqp沛沛1 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·1 天前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁1 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven1 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
程序猿编码1 天前
告别改源码适配模型:纯 C++ 可配置 LLM 推理引擎,全格式全结构兼容
c++·大模型·llm·推理引擎
此生决int1 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++