频道栏目
首页 > 程序开发 > 综合编程 > 其他综合 > 正文
大话设计模式C++实现-第25章-中介者模式
2015-01-07 10:38:32      个评论    来源:西青年·部落格  
收藏   我要投稿

一、UML

\



二、概念

中介者模式(Mediator):<喎"/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPtPD0ru49tbQvem21M/zwLS34tew0rvPtcHQtcS21M/zvbu7paGj1tC96dXfyrm497bUz/Oyu9Do0qrP1Mq+tdjP4Lul0v3Tw6OstNO2+Mq5xuTx7rrPy8nJoqOstvjH0r/J0tS2wMGitdi4xLHky/zDx9auvOS1xL27u6WhozwvcD4KPHA+PGJyPgo8L3A+CjxwPjxzdHJvbmc+yP2hosu1w/c8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+vcfJq6O6PC9zdHJvbmc+PC9wPgo8cD6jqDGjqUNvbGxlYWd1ZaO6s+nP882sysLA4KGjPC9wPgo8cD6jqDKjqUNvbmNyZXRlQ2xsZWFndWWjur7fzOXNrMrCwOCjrMO/uPa+38zlzazKwta71qq1wNfUvLq1xNDQzqqjrLb4srvBy73ixuTL+82sysLA4LXEx+m/9qOstavL/MPHyLS2vMjPyrbW0L3p1d+21M/zoaM8L3A+CjxwPqOoM6OpTWVkaWF0b3KjurPpz/PW0L3p1d+jrLao0uXBy82sysK21M/ztb3W0L3p1d+21M/ztcS907/aoaM8L3A+CjxwPqOoNKOpQ29uY3JldGVNZWRpYXRvcqO6vt/M5dbQvenV37bUz/OjrMq1z9az6c/zwOC1xLe9t6ijrMv80OjSqtaqtcDL+dPQvt/M5c2sysLA4KOssqK0077fzOXNrMrCvdPK1c/7z6KjrM/yvt/M5c2sysK21M/zt6Kz9sP8we6hozwvcD4KPHA+PHN0cm9uZz7W0L3p1d/Eo8q9tcTTxbXjo788L3N0cm9uZz48L3A+CjxwPqOoMaOpTWVkaWF0b3K1xLP2z9a89cnZwcu497j2Q29sbGVhZ3VltcTx7rrPo6zKubXDv8nS1LbAwaK12LjEseS6zbi008O497j2Q29sbGVhZ3Vlo6yxyMjnyM66zrn6vNK1xLjEseSyu7vh07DP7LW9xuTL+7n6vNKjrLb41rvKx9PrsLLA7bvht6LJ+rHku6+hozwvcD4KPHA+o6gyo6nTydPasNG21M/zyOe6ztCt1/e9+NDQwcuz6c/zo6y9q9bQvenX986q0ru49rbAwaK1xLjFxO6yor2rxuS34tewxvDU2tK7uPa21M/z1tCjrNXi0fm52NeitcS21M/zvs2007bUz/O499fUsb7J7bXE0NDOqteq0sa1vcv7w8fWrrzktcS9u7ulyc/AtKOs0rK+zcrH1b7U2tK7uPa4/Lrqudu1xL3HtsjIpb+0tP3Ptc2zoaM8L3A+CjxwPjxzdHJvbmc+1tC96dXfxKPKvbXEyLG146O/PC9zdHJvbmc+PC9wPgo8cD7TydPaQ29uY3JldGVNZWRpYXRvcr/Y1sbBy7yv1tC7r6Os09rKx77NsNG9u7uluLTU09DUseTOqsHL1tC96dXftcS4tNTT0NSjrNXivs3KubXD1tC96dXfu+Gx5LXDscjIzrrO0ru49kNvbmNyZXRlQ29sbGVhZ3Vltry4tNTToaM8L3A+CjxwPjxzdHJvbmc+1tC96dXfxKPKvbXE08PNvqO/PC9zdHJvbmc+PC9wPgo8cD7W0L3p1d/Eo8q90ruw49Om08PT2tK71+m21M/z0tS2qNLlwby6w7Wryse4tNTTtcS3vcq9vfjQ0M2o0MW1xLOhus+jrNLUvLDP67ao1sbSu7j2t9ayvNTatuC49sDg1tC1xNDQzqqjrLb409ayu8/ryfqzycyrtuC1xNfTwOC1xLOhus+hozwvcD4KPHA+PGJyPgo8L3A+CjxwPjxzdHJvbmc+y8ShokMmIzQzOyYjNDM7yrXP1jwvc3Ryb25nPjwvcD4KPHA+o6gxo6lVTi5oPC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">#ifndef _UN #define _UN #include 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 
#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 
#include "UN.h"
#include 

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 
#include 

using namespace std;

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

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

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

(5)运行截图


点击复制链接 与好友分享!回本站首页
上一篇:cocos3—2.js启动 js_boot.js
下一篇:VC++6.0下通过opencv读入图像并反色
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站