中介者模式是属于对象行为模式的一种设计模式。它的定义是:
用一个中介对象来封装一系列的的对象交互,中介者模式使得各个对象不需要显示的相互引用,从而使其耦合松散,而且可以独立的改变他们之间的交互。
中介者模式的结构图如下:
适用性
1 一组对象以定义良好但是复杂的方式进行通信,产生的相互依赖关系结构混乱并且难以理解
2 一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象
3 想定制一个分布在多个类中的行为,而不想生成太多的子类
协作
同事向一个中介者对象发送和接受请求,中介者在各个同事适当的转发请求以实现协作行为
优点
1 减少了子类生成
2 它将各个Colleague解耦
3 它简化了对象协议
4 它对对象如何协作进行了抽象
缺点
1 它使控制集中化,使得中介者由于系统的演变变得越来越复杂,使得难以维护
实现注意
1 忽略抽象的Mediator类,仅有一个中介者时,可以不使用抽象
2 Colleague—-Mediator的通信,
1 可使用观察者模式
2 定义一个特殊的通信接口,当通信直接调用该接口
例子
// MediatorModel.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "UnitedNationSecurity.h"
#include "USA.h"
#include "Iraq.h"
int _tmain(int argc, _TCHAR* argv[])
{
UnitedNationSecurity* unsc = new UnitedNationSecurity;
USA* usa = new USA(unsc);
Iraq* iraq = new Iraq(unsc);
unsc->setUsaCountry(usa);
unsc->setIraqCountry(iraq);
usa->declare("不准研制核武器,否则发动战争");
iraq->declare("我们没有核武器,不怕侵略");
delete usa;
usa = nullptr;
delete iraq;
iraq = nullptr;
delete unsc;
unsc = nullptr;
return 0;
}
/************************************************************************
@fileName:Country.h
@function: 中介者模式的抽象colleague类
@author: jaime
@ver: 1.0.0
************************************************************************/
#pragma once
class UnitedNations;
class Conutry
{
public:
Conutry(UnitedNations* nations);
~Conutry();
private:
protected:
UnitedNations* m_nations;
};
/************************************************************************
@fileName:USA.h
@function: 中介者模式的具体colleague类
@author: jaime
@ver: 1.0.0
************************************************************************/
#pragma once
#include "Country.h"
#include <string>
using namespace std;
class USA : public Conutry
{
public:
USA(UnitedNations* nations);
~USA();
void declare(string msg);
void getMsg(string msg);
private:
protected:
//UnitedNations* m_nations;
};
#include "USA.h"
#include "UnitedNations.h"
#include <iostream>
USA::USA(UnitedNations* nations)
:Conutry(nations)
{
}
USA::~USA()
{
}
void USA::declare(string msg)
{
m_nations->declare(msg, this);
}
void USA::getMsg(string msg)
{
cout << "USA msg is " << msg.c_str() << endl;
}
/************************************************************************
@fileName:Iraq.h
@function: 中介者模式的具体colleague类
@author: jaime
@ver: 1.0.0
************************************************************************/
#pragma once
#include "Country.h"
#include <string>
using namespace std;
class Iraq : public Conutry
{
public:
Iraq(UnitedNations* nations);
~Iraq();
void declare(string msg);
void getMsg(string msg);
private:
};
#include "Iraq.h"
#include "UnitedNations.h"
#include <iostream>
Iraq::Iraq(UnitedNations* nations)
:Conutry(nations)
{
}
Iraq::~Iraq()
{
}
void Iraq::declare(string msg)
{
m_nations->declare(msg, this);
}
void Iraq::getMsg(string msg)
{
cout << "Iraq msg is " << msg.c_str() << endl;
}
/************************************************************************
@fileName:UnitedNations.h
@function: 中介者模式的抽象中介者
@author: jaime
@ver: 1.0.0
************************************************************************/
#pragma once
#include <string>
#include "Country.h"
using namespace std;
class UnitedNations
{
public:
UnitedNations();
~UnitedNations();
virtual void declare(string message, Conutry* colleage) = 0;
private:
};
/************************************************************************
@fileName:UnitedNationSecurity.h
@function: 中介者模式的具体中介者类
@author: jaime
@ver: 1.0.0
************************************************************************/
#pragma once
#include "UnitedNations.h"
#include "USA.h"
#include "Iraq.h"
class UnitedNationSecurity : public UnitedNations
{
public:
UnitedNationSecurity();
~UnitedNationSecurity();
void setUsaCountry(USA* usa);
void setIraqCountry(Iraq* iraq);
virtual void declare(string message, Conutry* colleage);
private:
USA* m_usa;
Iraq* m_iraq;
};
#include "UnitedNationSecurity.h"
UnitedNationSecurity::UnitedNationSecurity()
{
}
UnitedNationSecurity::~UnitedNationSecurity()
{
}
void UnitedNationSecurity::setUsaCountry(USA* usa)
{
m_usa = usa;
}
void UnitedNationSecurity::setIraqCountry(Iraq* iraq)
{
m_iraq = iraq;
}
void UnitedNationSecurity::declare(string message, Conutry* colleage)
{
if (colleage == m_usa)
{
m_usa->getMsg(message);
}
else
{
m_iraq->getMsg(message);
}
}