Mediator.h
#ifndef _MEDIATOR_H_
#define _MEDIATOR_H_
#include <string>
using namespace std;
class CCollage;
class CMediator
{
public:
CMediator();
virtual ~CMediator();
virtual void DoActionFromAtoB()=0;
virtual void DoActionFromBtoA()=0;
};
class CConcreteMediator:public CMediator
{
public:
CConcreteMediator();
CConcreteMediator(CCollage *clgA,CCollage *clgB);
~CConcreteMediator();
void SetConcreteCollageA(CCollage*clgA);
void SetConcreteCollageB(CCollage*clgB);
CCollage *GetCollageA();
CCollage *GetCollageB();
void DoActionFromAtoB();
void DoActionFromBtoA();
void IntroCollage(CCollage *clgA,CCollage *clgB);
private:
CCollage *clgA;
CCollage *clgB;
};
class CCollage
{
public:
CCollage(CMediator *mdt);
CCollage();
virtual ~CCollage();
virtual void Action()=0;
virtual string GetState()=0;
virtual void SetState(const string &st)=0;
protected:
CMediator *mdt;
string st;
};
class CConcreteCollageA:public CCollage
{
public:
CConcreteCollageA();
CConcreteCollageA(CMediator *mdt);
~CConcreteCollageA();
void Action();
string GetState();
void SetState(const string &st);
};
class CConcreteCollageB:public CCollage
{
public:
CConcreteCollageB();
CConcreteCollageB(CMediator *mdt);
~CConcreteCollageB();
void Action();
string GetState();
void SetState(const string &st);
};
#endif
Mediator.cpp
#include <iostream>
#include "Meditaor.h"
using namespace std;
CMediator::CMediator()
{
}
CMediator::~CMediator()
{
}
CCollage::CCollage()
{
}
CCollage::CCollage(CMediator *mdt)
{
this->mdt=mdt;
}
CCollage::~CCollage()
{
}
CConcreteMediator::CConcreteMediator()
{
}
CConcreteMediator::CConcreteMediator(CCollage *clgA,CCollage *clgB)
{
this->clgA=clgA;
this->clgB=clgB;
}
CConcreteMediator::~CConcreteMediator()
{
}
void CConcreteMediator::SetConcreteCollageA(CCollage *clgA)
{
this->clgA=clgA;
}
void CConcreteMediator::SetConcreteCollageB(CCollage *clgB)
{
this->clgB=clgB;
}
void CConcreteMediator::DoActionFromBtoA()
{
clgA->SetState(clgB->GetState());
}
void CConcreteMediator::DoActionFromAtoB()
{
clgB->SetState(clgA->GetState());
}
CCollage *CConcreteMediator::GetCollageA()
{
return clgA;
}
CCollage *CConcreteMediator::GetCollageB()
{
return clgB;
}
void CConcreteMediator::IntroCollage(CCollage *clgA, CCollage *clgB)
{
this->clgA=clgA;
this->clgB=clgB;
}
CConcreteCollageA::CConcreteCollageA()
{
}
CConcreteCollageA::CConcreteCollageA(CMediator *mdt):CCollage(mdt)
{
this->mdt=mdt;
}
CConcreteCollageA::~CConcreteCollageA()
{
}
void CConcreteCollageA::SetState(const std::string &st)
{
this->st=st;
}
string CConcreteCollageA::GetState()
{
return st;
}
void CConcreteCollageA::Action()
{
mdt->DoActionFromAtoB();
cout<<"State of CollageB: "<<this->GetState()<<endl;
}
CConcreteCollageB::CConcreteCollageB()
{
}
CConcreteCollageB::CConcreteCollageB(CMediator *mdt):CCollage(mdt)
{
this->mdt=mdt;
}
CConcreteCollageB::~CConcreteCollageB()
{
}
void CConcreteCollageB::SetState(const std::string &st)
{
this->st=st;
}
string CConcreteCollageB::GetState()
{
return st;
}
void CConcreteCollageB::Action()
{
mdt->DoActionFromBtoA();
cout<<"State of CollageB: "<<this->GetState()<<endl;
}
Main.cpp
#include <iostream>
#include "Meditaor.h"
using namespace std;
int main()
{
CConcreteMediator *m=new CConcreteMediator();
CConcreteCollageA *c1=new CConcreteCollageA(m);
CConcreteCollageB *c2=new CConcreteCollageB(m);
m->IntroCollage(c1,c2);
c1->SetState("old");
c2->SetState("old");
c1->Action();
c2->Action();
cout<<endl;
c1->SetState("new");
c1->Action();
c2->Action();
cout<<endl;
c2->SetState("old");
c2->Action();
c1->Action();
if(m)
{
delete m;
m=0;
}
if(c1)
{
delete c1;
c1=0;
}
if(c2)
{
delete c2;
c2=0;
}
return 0;
}
Mediator模式是一种很有用并且很常用的模式,它通过将对象间的通信封装到一个类中,将多对多的通信转化为一对多的通信,降低了系统的复杂性。Mediator还获得系统解耦的特性,通过Mediator,各个Colleague就不必维护各自通信的对象和通信协议,降低了系统的耦合性,Mediator和各个Colleague就可以相互独立地修改了。
Mediator模式还有一个很显著额特点就是将控制集中,集中的优点就是便于管理,也正式符合了OO设计中的每个类的职责要单一和集中的原则。