中介者模式用一个中介对象来封装一系列的对象交互。中介者使各对象不须要显式地相互引用,从而使其耦合松散,并且能够独立地改变它们之间的交互。ios
中介者模式中,每一个同事对象维护一个中介者,当要进行通讯时,每一个具体的同事直接向中介者发信息,至于信息发到哪里,则由中介者来决定。设计模式
中介者模式就是迪米特法则的一个典型应用。在中介者模式中,经过创造出一个中介者对象,将系统中有关的对象所引用的其余对象数目减小到最少,使得一个对象与其同事之间的相互做用被这个对象与中介者对象之间的相互做用所取代。所以,中介者模式就是迪米特法则的一个典型应用。
经过引入中介者对象,能够将系统的网状结构变成以中介者为中心的星形结构,中介者承担了中转做用和协调做用。中介者类是中介者模式的核心,对整个系统进行控制和协调,简化了对象之间的交互,还能够对对象间的交互进行进一步的控制。ide
抽象中介者(Mediator):抽象中介者定义了同事对象到中介者对象的接口。
具体中介者(ConcreteMediator): 实现抽象类的方法,须要知道全部具体同事类,并从具体同事接收消息,向具体同事对象发出命令。具体中介者经过协调各同事对象实现协做行为。
抽象同事类(Colleague class): 定义同事类接口,定义各同事的公有方法.
具体同事类(ConcreteColleague): 实现抽象同事类中的方法。每个同事类须要知道中介者对象;每一个具体同事类只须要了解本身的行为,而不须要了解其余同事类的状况。每个同事对象在需与其余的同事通讯的时候,与它的中介者通讯。同事类之间必须经过中介者才能进行消息传递。this
中介者模式的优势:spa
A、适当地使用中介者模式能够避免同事类之间的过分耦合,使得各同事类之间能够相对独立地使用。设计
B、使用中介者模式能够将对象间一对多的关联转变为一对一的关联,使对象间的关系易于理解和维护。对象
C、使用中介者模式能够将对象的行为和协做进行抽象,可以比较灵活的处理对象间的相互做用。blog
D、使控制集中化。中介者模式将交互的复杂性变为中介者的复杂性接口
中介者模式的缺点:图片
控制集中化,使得中介者变得复杂而难以维护。
中介者模式使用场景:
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; }