设计模式(4)--对象行为(5)--中介者

1. 意图

    用一个中介对象来封装一系列的对象交互。

    中介者使各对象不需要显式地相互引用,从而使其耦合松散,

    而且可以独立地改变它们之间的交互。

2. 四种角色

    抽象中介者(Mediator)、具体中介者(Concrete Mediator)、抽象同事(Colleague)、

    具体同事(Concrete Colleague)

3. 优点

    3.1 减少了子类生成。

    3.2 将各个Colleague解耦。

    3.3 简化了对象协议。

    3.4 对对象如何协作进行了抽象。

4. 缺点

    4.1 使控制集中化,可能使中介者自身称为一个难于维护的庞然大物

5. 相关模式

    5.1 Mediator的协议是多向的,提供了Colleague对象间的协作行为;
          而Façade的协议是单向的,只能Façade对子系统提出请求。

    5.2 Colleague可使用Observer模式与Mediator通信。

6. 代码示意(C++)
#pragma once
#include 
#include 
#include 
using namespace std;

class Colleague;

class Mediator
{
public:
	virtual void SendMessage(const string& message, Colleague* pFrom) = 0;
	virtual void AddColleague(Colleague* pColleague) = 0;
};

class ConcreteMediator :public Mediator
{
	vector m_colleagues;
public:
	virtual void SendMessage(const string& message, Colleague* pFrom);
	virtual void AddColleague(Colleague* pColleague);
};

class Colleague
{
	string m_strName;
	Mediator* m_pMediator;
public:
	Colleague(Mediator* pMediator, const string& strName) {
		m_pMediator = pMediator;
		m_strName = strName;
	}
	void SendMessage(const string& message) {
		m_pMediator->SendMessage(message, this);
	}
	string GetName() { return m_strName; }

	virtual void GetMessage(const string& message, const string& name) = 0;
};

class ConcreteColleage1 :public Colleague
{
public:
	ConcreteColleage1(Mediator* pMediator) :Colleague(pMediator, "ConcreteColleage1") {

	}
	virtual void GetMessage(const string& message, const string& name) {
		cout << "ConcreteColleage1 got message:" << message << ",from:" << name << endl;
	}
};

class ConcreteColleage2 :public Colleague
{
public:
	ConcreteColleage2(Mediator* pMediator) :Colleague(pMediator, "ConcreteColleage2") {

	}
	virtual void GetMessage(const string& message, const string& name) {
		cout << "ConcreteColleage2 got message:" << message << ",from:" << name << endl;
	}
};

 Mediator.cpp:

#include "Mediator.h"

void ConcreteMediator::SendMessage(const string& message, Colleague* pFrom) {
	auto it = m_colleagues.begin();
	while (it != m_colleagues.end()) {
		if (pFrom != (*it)) {
			(*it)->GetMessage(message, pFrom->GetName());
		}
		++it;
	}
}
void ConcreteMediator::AddColleague(Colleague* pColleague) {
	m_colleagues.emplace_back(pColleague);
}
#include "Mediator.h"
int main() {
	Mediator* pMediator = new ConcreteMediator();

	Colleague* pColleague1 = new ConcreteColleage1(pMediator);
	Colleague* pColleague2 = new ConcreteColleage2(pMediator);
	pMediator->AddColleague(pColleague1);
	pMediator->AddColleague(pColleague2);

	pColleague1->SendMessage("hello");

	pColleague2->SendMessage("hi");

	delete pColleague1;
	delete pColleague2;
	delete pMediator;
	return 0;
}

运行结果:

 6.1 ConcreteColleage之间不直接打交道,消息通过ConcreteMediator传递(3.2)。

 6.2 将多对多的关系变成了一对多(一个中介对多个同事)的关系(3.3)。

你可能感兴趣的:(设计模式,设计模式)