(本博客旨在个人总结回顾)
中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
说明:
优点:
①降低了类的复杂度,将一对多转化成了一对一。
②各个类之间的解耦。
③符合迪米特原则。
缺点:中介者会庞大,变得复杂难以维护。
使用场景:
①系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象。
②想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。
注意事项:不应当在职责混乱的时候使用。
stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include
#include
#include
using namespace std;
// TODO: 在此处引用程序需要的其他头文件
Object.h
#pragma once
#include "Mediator.h"
class Mediator;
class Object
{
public:
Object(Mediator* pMediator);
virtual ~Object();
public:
virtual void Send(string strThing) = 0;
virtual void Receive(string strThing) = 0;
protected:
Mediator* m_pMediator;
};
Object.cpp
#include "stdafx.h"
#include "Object.h"
Object::Object(Mediator* pMediator)
: m_pMediator(pMediator)
{
}
Object::~Object()
{
}
ObjectX.h
#pragma once
#include "Object.h"
class ObjectX :
public Object
{
public:
ObjectX(Mediator* pMediator);
~ObjectX();
public:
void Send(string strThing);
void Receive(string strThing);
};
ObjectX.cpp
#include "stdafx.h"
#include "ObjectX.h"
ObjectX::ObjectX(Mediator* pMediator)
: Object(pMediator)
{
}
ObjectX::~ObjectX()
{
}
void ObjectX::Send(string strThing)
{
m_pMediator->Operation(strThing, this);
}
void ObjectX::Receive(string strThing)
{
cout << "ObjectX收到信息:" << strThing.c_str() << endl;
}
ObjectY.h
#pragma once
#include "Object.h"
class ObjectX :
public Object
{
public:
ObjectX(Mediator* pMediator);
~ObjectX();
public:
void Send(string strThing);
void Receive(string strThing);
};
ObjectY.cpp
#include "stdafx.h"
#include "ObjectY.h"
ObjectY::ObjectY(Mediator* pMediator)
: Object(pMediator)
{
}
ObjectY::~ObjectY()
{
}
void ObjectY::Send(string strThing)
{
m_pMediator->Operation(strThing, this);
}
void ObjectY::Receive(string strThing)
{
cout << "ObjectY收到信息:" << strThing.c_str() << endl;
}
Mediator.h(中介者类接口)
#pragma once
#include "Mediator.h"
class Mediator;
class Object
{
public:
Object(Mediator* pMediator);
virtual ~Object();
public:
virtual void Send(string strThing) = 0;
virtual void Receive(string strThing) = 0;
protected:
Mediator* m_pMediator;
};
Mediator.cpp
#include "stdafx.h"
#include "Mediator.h"
Mediator::Mediator()
{
}
Mediator::~Mediator()
{
}
TheMediator.h (具体中介者类)
#pragma once
#include "Mediator.h"
#include "ObjectX.h"
#include "ObjectY.h"
class TheMediator :
public Mediator
{
public:
TheMediator();
~TheMediator();
public:
void Operation(string strInfo, Object* pObject);
void SetObject(ObjectX* pObjectX, ObjectY* pObjectY);
private:
ObjectX* m_pObjectX;
ObjectY* m_pObjectY;
};
TheMediator.cpp
#include "stdafx.h"
#include "TheMediator.h"
TheMediator::TheMediator()
: m_pObjectX(NULL)
, m_pObjectY(NULL)
{
}
TheMediator::~TheMediator()
{
}
void TheMediator::SetObject(ObjectX* pObjectX, ObjectY* pObjectY)
{
if (m_pObjectX != NULL)
{
delete m_pObjectX;
m_pObjectX = NULL;
}
m_pObjectX = pObjectX;
if (m_pObjectY != NULL)
{
delete m_pObjectY;
m_pObjectY = NULL;
}
m_pObjectY = pObjectY;
}
void TheMediator::Operation(string strInfo, Object* pObject)
{
if (pObject != m_pObjectX)
{
m_pObjectX->Receive(strInfo);
}
else if (pObject != m_pObjectY)
{
m_pObjectY->Receive(strInfo);
}
}
调用代码
MediatorPatternMemo.cpp
// MediatorPatternMemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "TheMediator.h"
#include "ObjectX.h"
#include "ObjectY.h"
int _tmain(int argc, _TCHAR* argv[])
{
TheMediator* pTheMediator = new TheMediator();
ObjectX* pObjectX = new ObjectX(pTheMediator);
ObjectY* pObjectY = new ObjectY(pTheMediator);
pTheMediator->SetObject(pObjectX, pObjectY);
pObjectX->Send("在吗?");
pObjectY->Send("亲,干嘛!");
pObjectX->Send("借钱,~-~||");
pObjectY->Send("滚");
system("pause");
return 0;
}