C++ 全栈知识体系C++ 全栈知识体系
✿导航
  • 基础
  • 函数
  • 知识点
  • IO框架
  • 新版本特性
  • 数据库原理
  • SQL语言
  • SQL - MySQL
  • NoSQL - Redis
  • NoSQL - ElasticSearch
  • 算法基础
  • 常见算法
  • 领域算法
  • 分布式算法
  • 数据结构与算法
  • 计算机网络
  • 操作系统
  • 计算机组成
  • 开发
  • 测试
  • 架构基础
  • 分布式系统
  • 微服务
  • 中间件
  • 概念
  • 理论
  • 架构设计原则
  • 设计模式
  • 协议
  • 技术选型
  • 编码规范
  • 流水线构建 - CI/CD
  • 知识点 - Linux
  • 网站 - Nginx
  • 容器化 - Docker
  • 容器编排 - Kubernetes
  • 服务网格 - Service Mesh Istio
  • 常用快捷键 - Shortcut
  • 工具使用 - Tools
  • 开源项目
  • 学习项目
  • 个人项目
  • 项目开发
  • 项目Idea
  • 并发
  • 部署
  • 分布式
  • 知识
  • 问题
  • 编程语言与技术
  • 系统与架构
  • 软件开发实践
  • 数据处理与应用设计
  • 个人
  • 产品
  • 团队
  • 知识体系
  • Vue
关于
✿导航
  • 基础
  • 函数
  • 知识点
  • IO框架
  • 新版本特性
  • 数据库原理
  • SQL语言
  • SQL - MySQL
  • NoSQL - Redis
  • NoSQL - ElasticSearch
  • 算法基础
  • 常见算法
  • 领域算法
  • 分布式算法
  • 数据结构与算法
  • 计算机网络
  • 操作系统
  • 计算机组成
  • 开发
  • 测试
  • 架构基础
  • 分布式系统
  • 微服务
  • 中间件
  • 概念
  • 理论
  • 架构设计原则
  • 设计模式
  • 协议
  • 技术选型
  • 编码规范
  • 流水线构建 - CI/CD
  • 知识点 - Linux
  • 网站 - Nginx
  • 容器化 - Docker
  • 容器编排 - Kubernetes
  • 服务网格 - Service Mesh Istio
  • 常用快捷键 - Shortcut
  • 工具使用 - Tools
  • 开源项目
  • 学习项目
  • 个人项目
  • 项目开发
  • 项目Idea
  • 并发
  • 部署
  • 分布式
  • 知识
  • 问题
  • 编程语言与技术
  • 系统与架构
  • 软件开发实践
  • 数据处理与应用设计
  • 个人
  • 产品
  • 团队
  • 知识体系
  • Vue
关于
  • 概念

    • 概念 - 概述
    • 概念 - 计算机专有名词
    • 概念 - 正向代理和反向代理
    • 概念 - 云网络
    • 概念 - rest api
    • 概念 - 脑裂
  • 理论

    • 事务理论 - ACID
    • 分布式理论 - CAP
    • 分布式理论 - BASE
  • 架构设计原则

    • 架构设计原则 - 合适、简单、演化
    • 架构设计原则 - 高内聚、低耦合
    • 架构设计原则 - 正交四原则
    • 架构设计原则 - SOLID详解
    • 架构设计原则 - 分层架构MVC
    • 架构设计原则 - DDD领域驱动设计:贫血模型和充血模型
    • 架构设计原则 - DDD领域驱动设计
  • 设计模式

    • 创建型模式 - Create model

      • 创建型模式 - 单例模式(Singleton)
      • 创建型模式 - 工厂模式(Factory)
      • 创建型模式 - 抽象工厂(Abstract Factory)
      • 创建型模式 - 生成器(Builder)
      • 创建型模式 - 原型模式(Prototype)
    • 结构型模式 - Structural model

      • 结构型模式 - 外观(Facade)
      • 结构型模式 - 适配器(Adapter)
      • 结构型模式 - 桥接(Bridge)
      • 结构型模式 - 组合(Composite)
      • 结构型模式 - 装饰(Decorator)
      • 结构型模式 - 享元(Flyweight)
      • 结构型模式 - 代理(Proxy)
    • 行为型模式 - Behavioral model

      • 行为型模式 - 责任链(Chain Of Responsibility)
      • 行为型模式 - 策略(Strategy)
      • 行为型模式 - 模板模式(Template)
      • 行为型模式 - 命令模式(Command)
      • 行为型模式 - 观察者(Observer)
      • 行为型模式 - 访问者(Visitor)
      • 行为型模式 - 状态(State)
      • 行为型模式 - 解释器(Interpreter)
      • 行为型模式 - 迭代器(Iterator)
      • 行为型模式 - 中介者(Mediator)
      • 行为型模式 - 备忘录(Memento)
  • 协议

    • 协议 - Http
    • 协议 - SNMP
    • 协议 - NETCONF
    • 协议 - TLS和SSL
    • 协议 - Http-wiki
    • 协议 - TCP/IP
    • 协议 - Https常见的认证模式
  • 技术选型

    • 技术选型 - 常用的技术框架
    • 技术选型 - 如何写一个自己的项目
    • 技术选型 - 基于drogon实现用户中心后端
  • 编码规范

    • 编码规范 - Google C++ Style Guide
    • 编码规范 - 编程风格
    • 编码规范 - 头文件包含规范
    • 编码规范 - 常用编码命名规则
    • 编码规范 - 编码命名规范

行为型 - 中介者(Mediator)

中介者模式(Mediator pattern) : 使用中介者模式来集中相关对象之间复杂的沟通和控制方式。

​[[toc]]

概念

Mediator 模式典型的结构图为:

Mediator 模式中,每个 Colleague 维护一个 Mediator,当要进行交互,例如图中ConcreteColleagueA 和 ConcreteColleagueB 之间的交互就可以通过 ConcreteMediator 提供的DoActionFromAtoB 来处理, ConcreteColleagueA 和 ConcreteColleagueB 不必维护对各自的引用,甚至它们也不知道各个的存在。 Mediator 通过这种方式将多对多的通信简化为了一( Mediator) 对多( Colleague)的通信。

代码实现

#ifndef _COLLEAGE_H_
#define _COLLEAGE_H_
#include <string>
using namespace std;
class Mediator;
class Colleage
{
public:
    virtual ~Colleage();
    virtual void Aciton() = 0;
    virtual void SetState(const string &sdt) = 0;
    virtual string GetState() = 0;

protected:
    Colleage();
    Colleage(Mediator *mdt);
    Mediator *_mdt;

private:
};

class ConcreteColleageA : public Colleage
{
public:
    ConcreteColleageA();
    ConcreteColleageA(Mediator *mdt);
    ~ConcreteColleageA();
    void Aciton();
    void SetState(const string &sdt);
    string GetState();

protected:
private:
    string _sdt;
};

class ConcreteColleageB : public Colleage
{
public:
    ConcreteColleageB();
    ConcreteColleageB(Mediator *mdt);
    ~ConcreteColleageB();
    void Aciton();
    void SetState(const string &sdt);
    string GetState();

protected:
private:
    string _sdt;
};
#endif //~_COLLEAGE_H_

#include "Mediator.h"
#include "Colleage.h"
#include <iostream>
using namespace std;

Colleage::Colleage()
{
}

Colleage::Colleage(Mediator *mdt)
{
    this->_mdt = mdt;
}

Colleage::~Colleage()
{
}

ConcreteColleageA::ConcreteColleageA()
{
}

ConcreteColleageA::~ConcreteColleageA()
{
}
ConcreteColleageA::ConcreteColleageA(Mediator *mdt) : Colleage(mdt)
{
}

string ConcreteColleageA::GetState()
{
    return _sdt;
}

void ConcreteColleageA::SetState(const string &sdt)
{
    _sdt = sdt;
}

void ConcreteColleageA::Aciton()
{
    _mdt->DoActionFromAtoB();
    cout << "State of ConcreteColleageA: " << this->GetState() << endl;
}

ConcreteColleageB::ConcreteColleageB()
{
}

ConcreteColleageB::~ConcreteColleageB()
{
}

ConcreteColleageB::ConcreteColleageB(Mediator *mdt) : Colleage(mdt)
{
}

void ConcreteColleageB::Aciton()
{
    _mdt->DoActionFromBtoA();
    cout << "State of ConcreteColleageB: " << this->GetState() << endl;
}

string ConcreteColleageB::GetState()
{
    return _sdt;
}

void ConcreteColleageB::SetState(const string &sdt)
{
    _sdt = sdt;
}

#ifndef _MEDIATOR_H_
#define _MEDIATOR_H_
class Colleage;
class Mediator
{
public:
    virtual ~Mediator();
    virtual void DoActionFromAtoB() = 0;
    virtual void DoActionFromBtoA() = 0;

protected:
    Mediator();

private:
};

class ConcreteMediator : public Mediator
{
public:
    ConcreteMediator();
    ConcreteMediator(Colleage *clgA, Colleage *clgB);
    ~ConcreteMediator();
    void SetConcreteColleageA(Colleage *clgA);
    void SetConcreteColleageB(Colleage *clgB);
    Colleage *GetConcreteColleageA();
    Colleage *GetConcreteColleageB();
    void IntroColleage(Colleage *clgA, Colleage *clgB);
    void DoActionFromAtoB();
    void DoActionFromBtoA();

protected:
private:
    Colleage *_clgA;
    Colleage *_clgB;
};
#endif //~_MEDIATOR_H

#include "Mediator.h"
#include "Colleage.h"

Mediator::Mediator()
{
}

Mediator::~Mediator()
{
}

ConcreteMediator::ConcreteMediator()
{
}

ConcreteMediator::~ConcreteMediator()
{
}

ConcreteMediator::ConcreteMediator(Colleage *clgA, Colleage *clgB)
{
    this->_clgA = clgA;
    this->_clgB = clgB;
}

void ConcreteMediator::DoActionFromAtoB()
{
    _clgB->SetState(_clgA->GetState());
}

void ConcreteMediator::SetConcreteColleageA(Colleage *clgA)
{
    this->_clgA = clgA;
}

void ConcreteMediator::SetConcreteColleageB(Colleage *clgB)
{
    this->_clgB = clgB;
}

Colleage *ConcreteMediator::GetConcreteColleageA()
{
    return _clgA;
}

Colleage *ConcreteMediator::GetConcreteColleageB()
{
    return _clgB;
}

void ConcreteMediator::IntroColleage(Colleage *clgA, Colleage *clgB)
{
    this->_clgA = clgA;
    this->_clgB = clgB;
}

void ConcreteMediator::DoActionFromBtoA()
{
    _clgA->SetState(_clgB->GetState());
}

#include "Mediator.h"
#include "Colleage.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    ConcreteMediator *m = new ConcreteMediator();
    ConcreteColleageA *c1 = new ConcreteColleageA(m);
    ConcreteColleageB *c2 = new ConcreteColleageB(m);

    m->IntroColleage(c1, c2);
    c1->SetState("old");
    c2->SetState("old");
    c1->Aciton();
    c2->Aciton();
    cout << endl;

    c1->SetState("new");
    c1->Aciton();
    c2->Aciton();
    cout << endl;

    c2->SetState("old");
    c2->Aciton();
    c1->Aciton();
    return 0;
}

[root@VM-16-6-centos Mediator]# ./MediatorTest
State of ConcreteColleageA: old
State of ConcreteColleageB: old

State of ConcreteColleageA: new
State of ConcreteColleageB: new

State of ConcreteColleageB: old
State of ConcreteColleageA: old

代码说明

Mediator 模式的实现关键就是将对象 Colleague 之间的通信封装到一个类种单独处理,为了模拟 Mediator 模式的功能,这里给每个 Colleague 对象一个 string 型别以记录其状态,并通过状态改变来演示对象之间的交互和通信。这里主要就 Mediator 的示例运行结果给出

分析:

  1. 将 ConcreteColleageA 对象设置状态“ old”, ConcreteColleageB 也设置状态“ old”;
  2. ConcreteColleageA 对象改变状态,并在 Action 中和 ConcreteColleageB 对象进行通信,并改变ConcreteColleageB 对象的状态为“ new”;
  3. ConcreteColleageB 对象改变状态,并在 Action 中和 ConcreteColleageA 对象进行通信,并改变ConcreteColleageA 对象的状态为“ old”;

注意到,两个 Colleague 对象并不知道它交互的对象,并且也不是显示地处理交互过程,这一切都是通过 Mediator 对象完成的,示例程序运行的结果也正是证明了这一点。

讨论

Mediator 模式是一种很有用并且很常用的模式,它通过将对象间的通信封装到一个类中,将多对多的通信转化为一对多的通信, 降低了系统的复杂性(有点类似消息队列)。 Mediator 还获得系统解耦的特性,通过 Mediator,各个 Colleague 就不必维护各自通信的对象和通信协议,降低了系统的耦合性, Mediator 和各个 Colleague 就可以相互独立地修改了。Mediator 模式还有一个很显著额特点就是将控制集中,集中的优点就是便于管理,也正式符合了 OO 设计中的每个类的职责要单一和集中的原则

Last Updated:
Contributors: klc407073648
Prev
行为型模式 - 迭代器(Iterator)
Next
行为型模式 - 备忘录(Memento)