当前位置: 首页 > news >正文

设计模式之中介者模式(Mediator)的C++实现

1、中介者模式的提出

在软件组件开发过程中,如果存在多个对象,且这些对象之间存在的相互交互的情况不是一一对应的情况,这种功能组件间的对象引用关系比较复杂,耦合度较高。如果有一些新的需求变化,则不易扩展。中介者模式可以解决这种问题。中介者模式使用一个“中介对象”去管理对象间的关联关系,这个中介对象里面定义了对象间的数据统一交互接口,解耦对象间的直接交互依赖,从而提高代码的扩展性。

2、需求描述

有2个线上人员,这两个线上人员可以相互发送和接收对象的消息。设计一个线上人员数据交互的代码,且代码架构便于扩展。

3、中介者模式的代码实现

(1)UML图如下:

 

 (2)代码实现如下:

#include <iostream>
#include <vector>class Mediator;
class OnlinerPeople
{
protected:Mediator* mediator;
public:virtual void sendMsg(std::string msg)=0;virtual void receiveMsg(std::string msg)=0;virtual ~OnlinerPeople(){};
};class Mediator
{
protected:std::vector<OnlinerPeople*>vecOnlinePeoples;
public:virtual void sendMessage(std::string msg,OnlinerPeople* pepole)=0;virtual void addOnliners(OnlinerPeople* people)=0;virtual ~Mediator(){};
};class ConcreteMediator:public Mediator
{
public:void addOnliners(OnlinerPeople* people)override{vecOnlinePeoples.emplace_back(people);};virtual void sendMessage(std::string msg, OnlinerPeople *pepole) override{for(auto it =  vecOnlinePeoples.begin();it != vecOnlinePeoples.end(); it++){if( *it != pepole){(*it)->receiveMsg(msg);}}}
};class OnlinerPeople1:public OnlinerPeople
{
public:void Init(Mediator* mediator){this->mediator = mediator;}virtual void sendMsg(std::string msg) override{std::cout << "OnlinerPeople1 sendMsg: " << std::endl;mediator->sendMessage(msg,this);}virtual void receiveMsg(std::string msg) override{std::cout << "OnlinerPeople1 receive msg: " << msg << std::endl;}
};class OnlinerPeople2:public OnlinerPeople
{
public:void Init(Mediator* mediator){this->mediator = mediator;}virtual void sendMsg(std::string msg) override{std::cout << "OnlinerPeople2 sendMsg: " << std::endl;mediator->sendMessage(msg,this);}virtual void receiveMsg(std::string msg) override{std::cout << "OnlinerPeople2 receive msg: " << msg << std::endl;}
};
class Client
{
private:Mediator* mediator{nullptr};OnlinerPeople1 people1;OnlinerPeople2 people2;
public:Client(Mediator* p){mediator = p;people1.Init(mediator);people2.Init(mediator);};void DoWork(){mediator->addOnliners(&people1);mediator->addOnliners(&people2);people1.sendMsg("hello people2");people2.sendMsg("hello people1");};
};int main()
{Mediator* mediator = new ConcreteMediator();Client obj(mediator);obj.DoWork();delete mediator;mediator = nullptr;return 0;
}

 程序运行结果如下: 

http://www.lryc.cn/news/136030.html

相关文章:

  • css弹性布局的方式
  • 阿里云源 Python、npm、git、goproxy
  • 微服务架构1.0
  • iOS开发Swift-基础部分
  • 【LeetCode-经典面试150题-day11】
  • 深度学习入门(三):卷积神经网络(CNN)
  • 网站是如何识别网络爬虫的?
  • TP-Link 智能灯泡缺陷能让黑客窃取用户 WiFi 密码
  • 接口测试,如何测试?
  • React源码解析18(11)------ 实现多次setState的批处理
  • 评测凯迪仕K70「千里眼」智能锁:不忘安全初心,便捷体验更上一层
  • mysql数据库root密码遗忘后,修改root密码
  • 网络安全(黑客)快速入门~
  • 华为OD机试 - 数字颠倒(Java 2023 B卷 100分)
  • leetcode做题笔记87扰乱字符串
  • 第一章 初识Linux(含VMware安装Ubuntu、CentOS、Windows、FinalShell、快照)
  • MATLAB算法实战应用案例精讲-【图像处理】OCR识别方法-CRNN
  • 无涯教程-PHP - preg_grep()函数
  • 【Linux】Nginx解决跨域问题
  • 无涯教程-PHP - preg_split()函数
  • B. Spreadsheets
  • matlab面向对象
  • 01、Cannot resolve MVC View ‘xxxxx前端页面‘
  • 时空智友企业流程化管控系统文件上传漏洞复现
  • 【已解决】Authenticator:无法添加账户请验证激活代码是否正确以及您的设备是否已为此应用启用推送通知
  • 聊聊springboot tomcat的maxHttpFormPostSize
  • java并发:synchronized锁详解
  • Unity 之NavMeshAgent 组件(导航和路径寻找的组件)
  • 装箱和拆箱
  • 等保测评--安全通信网络--测评方法