中介者模式将多对多的交互简化为一对多的交互,引入一个中介者使其对象耦合松散,而且可以独立的改变它们之间的交互。
mediator.h
#include <vector>
class CPerson;
class CMediator
{
public:
CMediator(){}
virtual ~CMediator(){}
void Register(CPerson* aoPerson);
CPerson* FindPartner(CPerson* aoPerson);
private:
std::vector<CPerson*> moVecBoyPerson;
std::vector<CPerson*> moVecGirlPerson;
};
mediator.cpp
#include "mediator.h"
#include "person.h"
void CMediator::Register(CPerson* aoPerson)
{
CBoy* lpoBoy = dynamic_cast<CBoy*>(aoPerson);
if (lpoBoy != NULL)
{
moVecBoyPerson.push_back(aoPerson);
}
else
{
moVecGirlPerson.push_back(aoPerson);
}
}
CPerson* CMediator::FindPartner(CPerson* aoPerson)
{
CBoy* lpoBoy = dynamic_cast<CBoy*>(aoPerson);
if (lpoBoy != NULL)
{
std::vector<CPerson*>::iterator loIter = moVecGirlPerson.begin();
for (; loIter != moVecGirlPerson.end(); ++loIter)
{
if (aoPerson->GetCondition() == (*loIter)->GetCondition())
{
return (*loIter);
}
}
}
else
{
std::vector<CPerson*>::iterator loIter = moVecBoyPerson.begin();
for (; loIter != moVecBoyPerson.end(); ++loIter)
{
if (aoPerson->GetCondition() == (*loIter)->GetCondition())
{
return (*loIter);
}
}
}
return NULL;
}
person.h
#include <string>
class CMediator;
class CPerson
{
public:
CPerson(CMediator* apoMedia, std::string aoName, int aiCond):mpoMedia(apoMedia),moName(aoName),miCond(aiCond)
{
}
virtual ~CPerson(){}
virtual CPerson* FindPartner(CPerson* aoPerson) = 0;
void SetConditon(int aiCond)
{
miCond = aiCond;
}
int GetCondition()
{
return miCond;
}
std::string GetName()
{
return moName;
}
private:
std::string moName;
int miCond;
protected:
CMediator* mpoMedia;
};
class CBoy: public CPerson
{
public:
CBoy(CMediator* apoMedia, std::string aoName, int aiCond);
virtual ~CBoy(){}
CPerson* FindPartner(CPerson* aoPerson);
};
class CGirl: public CPerson
{
public:
CGirl(CMediator* apoMedia, std::string aoName, int aiCond);
virtual ~CGirl(){}
CPerson* FindPartner(CPerson* aoPerson);
};
person.cpp
#include "person.h"
#include "mediator.h"
CBoy::CBoy(CMediator* apoMedia, std::string aoName, int aiCond):CPerson(apoMedia, aoName, aiCond)
{
mpoMedia->Register(this);
}
CPerson* CBoy::FindPartner(CPerson* aoPerson)
{
return mpoMedia->FindPartner(aoPerson);
}
CGirl::CGirl(CMediator* apoMedia, std::string aoName, int aiCond):CPerson(apoMedia, aoName, aiCond)
{
mpoMedia->Register(this);
}
CPerson* CGirl::FindPartner(CPerson* aoPerson)
{
return mpoMedia->FindPartner(aoPerson);
}
mediatorTest.cpp
#include "person.h"
#include "mediator.h"
#include <stdio.h>
int main(int argv, char* argc[])
{
CMediator* lpoMedia = new CMediator();
CBoy* lpoBoy = new CBoy(lpoMedia, "张三", 6);
CGirl*lpoGirl1 = new CGirl(lpoMedia, "李沁", 7);
CGirl*lpoGirl2 = new CGirl(lpoMedia, "高媛媛", 6);
CPerson* lpoPer = lpoGirl1->FindPartner(lpoGirl1);
if (lpoPer != NULL)
{
printf("李沁匹配到 name: %s\n", lpoPer->GetName().c_str());
}
lpoPer = lpoGirl2->FindPartner(lpoGirl2);
{
printf("高媛媛匹配到 name: %s\n", lpoPer->GetName().c_str());
}
delete lpoGirl2;
delete lpoGirl1;
delete lpoBoy;
delete lpoMedia;
return 0;
}