设计模式(二十三)——中介者模式
一、中介者模式简介
1、中介者模式简介
中介者模式用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
中介者模式中,每个同事对象维护一个中介者,当要进行通信时,每个具体的同事直接向中介者发信息,至于信息发到哪里,则由中介者来决定。
中介者模式就是迪米特法则的一个典型应用。在中介者模式中,通过创造出一个中介者对象,将系统中有关的对象所引用的其他对象数目减少到最少,使得一个对象与其同事之间的相互作用被这个对象与中介者对象之间的相互作用所取代。因此,中介者模式就是迪米特法则的一个典型应用。
通过引入中介者对象,可以将系统的网状结构变成以中介者为中心的星形结构,中介者承担了中转作用和协调作用。中介者类是中介者模式的核心,对整个系统进行控制和协调,简化了对象之间的交互,还可以对对象间的交互进行进一步的控制。
2、中介者模式角色
抽象中介者(Mediator):抽象中介者定义了同事对象到中介者对象的接口。
具体中介者(ConcreteMediator): 实现抽象类的方法,需要知道所有具体同事类,并从具体同事接收消息,向具体同事对象发出命令。具体中介者通过协调各同事对象实现协作行为。
抽象同事类(Colleague class): 定义同事类接口,定义各同事的公有方法.
具体同事类(ConcreteColleague): 实现抽象同事类中的方法。每一个同事类需要知道中介者对象;每个具体同事类只需要了解自己的行为,而不需要了解其他同事类的情况。每一个同事对象在需与其他的同事通信的时候,与它的中介者通信。同事类之间必须通过中介者才能进行消息传递。
3、中介者模式优缺点
中介者模式的优点:
A、适当地使用中介者模式可以避免同事类之间的过度耦合,使得各同事类之间可以相对独立地使用。
B、使用中介者模式可以将对象间一对多的关联转变为一对一的关联,使对象间的关系易于理解和维护。
C、使用中介者模式可以将对象的行为和协作进行抽象,能够比较灵活的处理对象间的相互作用。
D、使控制集中化。中介者模式将交互的复杂性变为中介者的复杂性
中介者模式的缺点:
控制集中化,使得中介者变得复杂而难以维护。
4、中介者模式使用场景
中介者模式使用场景:
A、系统中对象之间存在复杂的引用关系,产生的相互依赖关系结构混乱且难以理解
B、一个对象由于引用了其他很多对象并且直接和这些对象通信,导致难以复用该对象
C、想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类
二、中介者模式实现
Mediator抽象中介者:
#ifndef MEDIATOR_H
#define MEDIATOR_H
#include <string>
#include <iostream>
using namespace std;
class Colleage;
//抽象中介者类
class Mediator
{
public:
//向同事发送消息接口
virtual void sendMessage(string msg, Colleage* colleage) = 0;
protected:
Mediator(){}
};
#endif // MEDIATOR_H
ConcreteMediator具体中介者:
#ifndef CONCRETEMEDIATOR_H
#define CONCRETEMEDIATOR_H
#include "Mediator.h"
#include "Colleage.h"
//具体中介者类
class ConcreteMediator : public Mediator
{
public:
virtual void sendMessage(string msg, Colleage* colleage)
{
if(colleage == m_pColleageA)
{ //如果发送消息的为同事A,则由同事B接收消息
m_pColleageB->getMessage(msg);
}
else if(colleage == m_pColleageB)
{
//如果发送消息的为同事B,则由同事A接收消息
m_pColleageA->getMessage(msg);
}
}
void setColleageA(Colleage* colleage)
{
m_pColleageA = colleage;
}
void setColleageB(Colleage* colleage)
{
m_pColleageB = colleage;
}
private:
Colleage* m_pColleageA;//同事A
Colleage* m_pColleageB;//同事B
};
#endif // CONCRETEMEDIATOR_H
Colleage抽象同事:
#ifndef COLLEAGE_H
#define COLLEAGE_H
#include "Mediator.h"
//抽象同事类
class Colleage
{
public:
//设置者
void setMediator(Mediator* mediator)
{
m_pMediator = mediator;
}
//向中介者发送消息接口
virtual void sendMessage(string msg) = 0;
//从中介者获取消息接口
virtual void getMessage(string msg) = 0;
protected:
Colleage(Mediator* mediator)
{
m_pMediator = mediator;
}
protected:
Mediator* m_pMediator;//中介
};
#endif // COLLEAGE_H
ConcreteColleageA具体同事类:
#ifndef CONCRETECOLLEAGEA_H
#define CONCRETECOLLEAGEA_H
#include "Colleage.h"
//具体同事类
class ConcreteColleageA : public Colleage
{
public:
ConcreteColleageA(Mediator* mediator):Colleage(mediator)
{
}
void sendMessage(string msg)
{
//要发送的消息由中介者转发
m_pMediator->sendMessage(msg, this);
}
void getMessage(string msg)
{
cout << "ConcreteColleageA receive an message: " << msg << endl;
}
};
#endif // CONCRETECOLLEAGEA_H
ConcreteColleageB具体同事类:
#ifndef CONCRETECOLLEAGEB_H
#define CONCRETECOLLEAGEB_H
#include "Colleage.h"
//具体同事类
class ConcreteColleageB : public Colleage
{
public:
ConcreteColleageB(Mediator* mediator):Colleage(mediator)
{
}
void sendMessage(string msg)
{
//要发送的消息由中介者转发
m_pMediator->sendMessage(msg, this);
}
void getMessage(string msg)
{
cout << "ConcreteColleageB receive an message: " << msg << endl;
}
};
#endif // CONCRETECOLLEAGEB_H
客户调用程序:
#include "Mediator.h"
#include "Colleage.h"
#include "ConcreteMediator.h"
#include "ConcreteColleageA.h"
#include "ConcreteColleageB.h"
using namespace std;
int main()
{
//创建中介者
ConcreteMediator* mediator = new ConcreteMediator();
//创建同事
Colleage* coleageA = new ConcreteColleageA(mediator);
Colleage* coleageB = new ConcreteColleageB(mediator);
//设置中介者管理的同事对象
mediator->setColleageA(coleageA);
mediator->setColleageB(coleageB);
//同事发送消息
coleageA->sendMessage("A");
coleageB->sendMessage("B");
delete mediator;
delete coleageA;
delete coleageB;
return 0;
}
转载于:https://blog.51cto.com/9291927/1970495