大话设计模式C++实现-第25章-中介者模式

一、UML




二、概念

中介者模式(Mediator):用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。


三、说明

角色:

(1)Colleague:抽象同事类。

(2)ConcreteClleague:具体同事类,每个具体同事只知道自己的行为,而不了解其他同事类的情况,但它们却都认识中介者对象。

(3)Mediator:抽象中介者,定义了同事对象到中介者对象的接口。

(4)ConcreteMediator:具体中介者对象,实现抽象类的方法,它需要知道所有具体同事类,并从具体同事接收消息,向具体同事对象发出命令。

中介者模式的优点?

(1)Mediator的出现减少了各个Colleague的耦合,使得可以独立地改变和复用各个Colleague,比如任何国家的改变不会影响到其他国家,而只是与安理会发生变化。

(2)由于把对象如何协作进行了抽象,将中介作为一个独立的概念并将其封装起在一个对象中,这样关注的对象就从对象各自本身的行为转移到他们之间的交互上来,也就是站在一个更宏观的角度去看待系统。

中介者模式的缺点?

由于ConcreteMediator控制了集中化,于是就把交互复杂性变为了中介者的复杂性,这就使得中介者会变得比任何一个ConcreteColleague都复杂。

中介者模式的用途?

中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通信的场合,以及想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。


四、C++实现

(1)UN.h

#ifndef _UN
#define _UN
#include <string>
class Country;
using namespace std;

//Mediator
class UnitedNations
{
public:
	virtual void setColleague1(Country* c)=0;
	virtual void setColleague2(Country* c)=0;
	virtual void Declare(string message, Country* colleague)=0;
};

//ConcreteMediator
class UnitedNationsSecurityCouncil:public UnitedNations
{
private:
	Country* colleague1;
	Country* colleague2;

public:
	void setColleague1(Country* c);
	void setColleague2(Country* c);
	void Declare(string message, Country* colleague);
};

#endif


(2)UN.cpp

#include <string>
#include "UN.h"
#include "Country.h"
using namespace std;

void UnitedNationsSecurityCouncil::setColleague1(Country* c)
{
	this->colleague1=c;
}

void UnitedNationsSecurityCouncil::setColleague2(Country* c)
{
	this->colleague2=c;
}

void UnitedNationsSecurityCouncil::Declare(string message, Country* colleague)
{
	if(colleague==this->colleague1)
		colleague2->GetMessage(message);
	else
		colleague1->GetMessage(message);
}


(3)Country.h

#ifndef _COUNTRY
#define _COUNTRY

#include <string>
#include "UN.h"
#include <iostream>

using namespace std;

//Colleague
class Country
{
protected:
	UnitedNations* mediator;
public:
	/*Country(UnitedNations* m)
	{
		this->mediator=m;
	}*/
	virtual void Declare(string message)=0;
	virtual void GetMessage(string message)=0;
};

//ConcreteColleague
class USA:public Country
{
public:
	USA(UnitedNations* m)
	{
		this->mediator=m;
	}
	void Declare(string message);
	void GetMessage(string message);
};

//ConcreteColleague
class Iraq:public Country
{
public:
	Iraq(UnitedNations* m)
	{
		this->mediator=m;
	}
	void Declare(string message);
	void GetMessage(string message);
};

#endif


(4)Country.cpp

#include "Country.h"
#include <string>
#include <iostream>

using namespace std;

void USA::Declare(string message)
{
	mediator->Declare(message,this);
}

void USA::GetMessage(string message)
{
	cout<<"美国获得对方信息:"<<message<<endl;
}

void Iraq::Declare(string message)
{
	mediator->Declare(message,this);
}

void Iraq::GetMessage(string message)
{
	cout<<"伊拉克获得对方信息:"<<message<<endl;
}



(5)运行截图


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值