Mediator中介者模式
作用:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

UML如下:

Colleage抽象同事类,而ConcreteColleage是具体同时类,每个具体同事只知道自己的行为,而不了解其他同事类的情况,但它们却都认识中介者对象,Mediator是抽象中介者,定义了同事对象到中介者对象的接口,ConcreteMediator是具体中介者对象,实现抽象类的方法,它需要知道所有具体同事类,并从具体同事接受消息,向具体同事对象发出命令。

Colleage类,抽象同事类

Mediator,抽象中介者类

说明:

1. Mediator 模式中,每个Colleague 维护一个 Mediator,当要进行通信时,每个具体的 Colleague 直接向ConcreteMediator 发信息,至于信息发到哪里,则由 ConcreteMediator 来决定。

2. ConcreteColleagueA 和 ConcreteColleagueB 不必维护对各自的引用,甚至它们也不知道各个的存在。

3. 优点是,各个 Colleague 减少了耦合。

4. 缺点是,由于 Mediator 控制了集中化,于是就把 Colleague 之间的交互复杂性变为了中介者的复杂性,也就是中介者会变的比任何一个 Colleague 都复杂。

中介者模式很容易在系统中应用,也很容易在系统中误用。当系统中出现了“多对多”交互复杂的对象群时,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。

Mediator的出现减少了各个Colleage的耦合,使得可以独立地改变和复用各个Colleage类和Mediator;
由于把对象如何协作进行了抽象,将中介作为一个独立的概念并将其封装在一个对象中,这样关注的对象就从对象各自本身的行为转移到它们之间的交互上来,也就是站在一个更宏观的角度去看待系统。

由于ConcreteMediator控制了集中化,于是就把交互复杂性变为了中介者的复杂性,这使得中介者会变得比任何一个ConcreteColleage都复杂。

中介者模式的优点来自集中控制,其缺点也是它。

中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通信的场合。

代码如下:

Mediator.h

 #ifndef _MEDIATOR_H_
#define _MEDIATOR_H_ #include <string> using namespace std; class Mediator; class Colleage
{
public:
virtual ~Colleage();
virtual void SetMediator(Mediator*);
virtual void SendMsg(string) = ;
virtual void GetMsg(string) = ;
protected:
Colleage(Mediator*);
Mediator* _mediator;
private: }; class ConcreteColleageA : public Colleage
{
public:
~ConcreteColleageA();
ConcreteColleageA(Mediator*);
virtual void SendMsg(string msg);
virtual void GetMsg(string);
protected:
private:
}; class ConcreteColleageB : public Colleage
{
public:
~ConcreteColleageB();
ConcreteColleageB(Mediator*);
virtual void SendMsg(string msg);
virtual void GetMsg(string);
protected:
private:
}; class Mediator
{
public:
virtual ~Mediator();
virtual void SendMsg(string,Colleage*) = ;
protected:
Mediator();
private:
}; class ConcreteMediator : public Mediator
{
public:
ConcreteMediator();
~ConcreteMediator();
void SetColleageA(Colleage*);
void SetColleageB(Colleage*);
virtual void SendMsg(string msg,Colleage*);
protected:
private:
Colleage* m_ColleageA;
Colleage* m_ColleageB;
};
#endif

Mediator.cpp

 #include "Mediator.h"
#include <iostream>
#include <string> using namespace std; Colleage::Colleage(Mediator* pMediator)
{
this->_mediator = pMediator;
} Colleage::~Colleage()
{} void Colleage::SetMediator(Mediator* pMediator)
{
this->_mediator = pMediator;
} ConcreteColleageA::ConcreteColleageA(Mediator* pMediator) : Colleage(pMediator)
{
} ConcreteColleageA::~ConcreteColleageA()
{
} void ConcreteColleageA::SendMsg(string msg)
{
this->_mediator->SendMsg(msg,this);
} void ConcreteColleageA::GetMsg(string msg)
{
cout << "ConcreteColleageA Receive:"<< msg << endl;
} ConcreteColleageB::ConcreteColleageB(Mediator* pMediator) : Colleage(pMediator)
{
} ConcreteColleageB::~ConcreteColleageB()
{
} void ConcreteColleageB::SendMsg(string msg)
{
this->_mediator->SendMsg(msg,this);
} void ConcreteColleageB::GetMsg(string msg)
{
cout << "ConcreteColleageB Receive:" << msg << endl;
} Mediator::Mediator()
{} Mediator::~Mediator()
{} ConcreteMediator::ConcreteMediator()
{} ConcreteMediator::~ConcreteMediator()
{} void ConcreteMediator::SetColleageA(Colleage* p)
{
this->m_ColleageA = p;
} void ConcreteMediator::SetColleageB(Colleage* p)
{
this->m_ColleageB = p;
} void ConcreteMediator::SendMsg(string msg,Colleage* p)
{
if(p == this->m_ColleageA)
{
this->m_ColleageB->GetMsg(msg);
}
else if(p == this->m_ColleageB)
{
this->m_ColleageA->GetMsg(msg);
}
}

main.cpp

 #include "Mediator.h"

 int main()
{
ConcreteMediator* pMediator = new ConcreteMediator(); Colleage* p1 = new ConcreteColleageA(pMediator);
Colleage* p2 = new ConcreteColleageB(pMediator); pMediator->SetColleageA(p1);
pMediator->SetColleageB(p2); p1->SendMsg("xxx");
p2->SendMsg("ooo");
return ;
}

结果如下:

C++设计模式-Mediator中介者模式的更多相关文章

  1. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...

  2. 设计模式16:Mediator 中介者模式(行为型模式)

    Mediator 中介者模式(行为型模式) 依赖关系的转化 动机(Motivation) 在软件构建过程中,经常出现多个对象互相关联交互的情况,对象之间经常会维持一种复杂的应用关系,如果遇到一些需求的 ...

  3. 设计模式之中介者模式(Mediator)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程.它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  4. 折腾Java设计模式之中介者模式

    博文原址:折腾Java设计模式之中介者模式 中介者模式 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性.这种模式提供了一个中介类,该类通常处理不同类之间的通信,并 ...

  5. java设计模式之中介者模式

    中介者模式 用一个中介对象来封装一系列的对象交互.中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式UML图 中介者模式代码 package com ...

  6. js设计模式——8.中介者模式

    js设计模式——8.中介者模式 /*js设计模式——中介者模式*/ class A { constructor() { this.number = 0; } setNumber(num, m) { t ...

  7. [设计模式] 17 中介者模式 Mediator Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变 ...

  8. 设计模式之中介者模式(Mediator )

    中介者模式是关于数据交互的设计模式,该模式的核心是一个中介者对象,负责协调一系列对象之间的不同的数据请求,这一系列对象成为同事类.如房产中介(简直不想提它),买房的卖房的,租房的放租的都到房产中介那里 ...

  9. C#设计模式:中介者模式(Mediator Pattern)

    一,什么是中介者模式(Mediator Pattern)? 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性.比如:如果我们实现两个人的交互,难道我们要定义两个对象 ...

随机推荐

  1. 在Android Studio中使用lambda表达式

    build.gradle中添加以下配置 Android{ ..... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targ ...

  2. Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)

    本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...

  3. php 获取静态方法调用的类名

    方法一: class foo { static public function test() { var_dump(get_called_class()); } } class bar extends ...

  4. JS实现HashMap

    /** * ********* 操作实例 ************** * var map = new HashMap(); * map.put("key1","Valu ...

  5. 开始写Effective系列总结一些前端的心得

    确实是没有时间整理以及总结和发表自己的感慨.难得中秋银行的事情搞完了自己清闲3天,是时候总结一下从大公司做.NET PC 端网站的开发到现在做移动互联网的银行及政府微信公众号的开发的感触.当时自己的选 ...

  6. 用unity4.3发布WINDOWS STORE APP应用的方法

    http://www.cnblogs.com/suxsho/ 原创,转载请声明 ============================================================ ...

  7. Dos学习笔记(3)attrib命令

    今天和昨天一直在摸索这个命令觉得这个命令为什么改变不了文件夹的属性, 因为我试着用attrib +r /s 去修改子文件夹的时候发现没用,然后如果输入 attrib +r /d 又提示说/d需要和/s ...

  8. 修改 C:\Users\[account name] 目录名称

    起因: 修改了用户名(第二个用户,标准用户,从 控制面板——用户账户 修改),后来发现 C:\Users\ 下的文件夹名称未变. 修改了 3 处: 1. 计算机——管理——本地用户和组——用户 2. ...

  9. 【耐克】【空军一号 Nike Air Force 1】【软木塞】

     [高帮 全白 36-45] [空军一号 低帮 36-46] [空军一号 36-45] [Nike Air Force 1 Flyknit 空军中帮飞线系列 全黑 36-44] [耐克空军一号 软木塞 ...

  10. 在树莓派上使用no-ip动态域名的方法,也适用其它Linux平台

    注意,如果没有公网IP,本文的方法就不可行了. 首先,注册一个noip.com的帐号. 注册的步骤见这篇教程:http://www.cnblogs.com/infopi/p/3991407.html ...