c++设计模式(19)-Mediator模式
来源:互联网 发布:枪械百科软件 编辑:程序博客网 时间:2024/06/05 17:08
Mediator模式典型的结构图为:
图2-1:Mediator Pattern结构图
Mediator模式中,每个Colleague维护一个Mediator,当要进行交互,例如图中ConcreteColleagueA和ConcreteColleagueB之间的交互就可以通过ConcreteMediator提供的DoActionFromAtoB来处理,ConcreteColleagueA和ConcreteColleagueB不必维护对各自的引用,甚至它们也不知道各个的存在。Mediator通过这种方式将多对多的通信简化为了一(Mediator)对多(Colleague)的通信。
2.3 实现
2.3.1 完整代码示例(code)
Mediator模式实现不是很困难,这里为了方便初学者的学习和参考,将给出完整的实现代码(所有代码采用C++实现,并在VC 6.0下测试运行)。 代码片断1:Colleage.h #ifndef _COLLEAGE_H_ #include <string> class Mediator; class Colleage virtual void Aciton() = 0; virtual void SetState(const string& sdt) = 0; virtual string GetState() = 0; Colleage(Mediator* mdt); Mediator* _mdt; private: }; class ConcreteColleageA:public Colleage ConcreteColleageA(Mediator* mdt); ~ConcreteColleageA(); void Aciton(); void SetState(const string& sdt); string GetState(); protected: private: }; class ConcreteColleageB:public Colleage ConcreteColleageB(Mediator* mdt); ~ConcreteColleageB(); void Aciton(); void SetState(const string& sdt); string GetState(); protected: private: }; #endif //~_COLLEAGE_H_ 代码片断2:Colleage.cpp #include "Mediator.h" #include "Colleage.h" #include <iostream> Colleage::Colleage() Colleage::Colleage(Mediator* mdt) //_sdt = " "; Colleage::~Colleage() } ConcreteColleageA::ConcreteColleageA() } ConcreteColleageA::~ConcreteColleageA() } ConcreteColleageA::ConcreteColleageA(Mediator* mdt):Colleage(mdt) } string ConcreteColleageA::GetState() void ConcreteColleageA::SetState(const string& sdt) void ConcreteColleageA::Aciton() cout<<"State of ConcreteColleageB:"<<" "<<this->GetState()<<endl; ConcreteColleageB::ConcreteColleageB() } ConcreteColleageB::~ConcreteColleageB() } ConcreteColleageB::ConcreteColleageB(Mediator* mdt):Colleage(mdt) } void ConcreteColleageB::Aciton() cout<<"State of ConcreteColleageB:"<<" "<<this->GetState()<<endl; string ConcreteColleageB::GetState() void ConcreteColleageB::SetState(const string& sdt) 代码片断3:Mediator.h #ifndef _MEDIATOR_H_ class Colleage; class Mediator virtual void DoActionFromAtoB() = 0; virtual void DoActionFromBtoA() = 0; protected: private: }; class ConcreteMediator:public Mediator ConcreteMediator(Colleage* clgA,Colleage* clgB); ~ConcreteMediator(); void SetConcreteColleageA(Colleage* clgA); void SetConcreteColleageB(Colleage* clgB); Colleage* GetConcreteColleageA(); Colleage* GetConcreteColleageB(); void IntroColleage(Colleage* clgA,Colleage* clgB); void DoActionFromAtoB(); void DoActionFromBtoA(); protected: private: Colleage* _clgB; }; #endif //~_MEDIATOR_H_ 代码片断4:Mediator.cpp #include "Mediator.h" #include "Colleage.h" Mediator::Mediator() } Mediator::~Mediator() } ConcreteMediator::ConcreteMediator() } ConcreteMediator::~ConcreteMediator() } ConcreteMediator::ConcreteMediator(Colleage* clgA,Colleage* clgB) this->_clgB = clgB; void ConcreteMediator::DoActionFromAtoB() void ConcreteMediator::SetConcreteColleageA(Colleage* clgA) void ConcreteMediator::SetConcreteColleageB(Colleage* clgB) Colleage* ConcreteMediator::GetConcreteColleageA() Colleage* ConcreteMediator::GetConcreteColleageB() void ConcreteMediator::IntroColleage(Colleage* clgA,Colleage* clgB) this->_clgB = clgB; void ConcreteMediator::DoActionFromBtoA() 代码片断5:main.cpp #include "Mediator.h" #include "Colleage.h" #include <iostream> int main(int argc,char* argv[]) ConcreteColleageA* c1 = new ConcreteColleageA(m); m->IntroColleage(c1,c2); c1->SetState("old"); c1->SetState("new"); c2->SetState("old"); return 0;
//Colleage.h
#define _COLLEAGE_H_
using namespace std;
{
public:
virtual ~Colleage();
protected:
Colleage();
{
public:
ConcreteColleageA();
string _sdt;
{
public:
ConcreteColleageB();
string _sdt;
//Colleage.cpp
using namespace std;
{
//_sdt = " ";
}
{
this->_mdt = mdt;
}
{
{
{
{
{
return _sdt;
}
{
_sdt = sdt;
}
{
_mdt->DoActionFromAtoB();
}
{
{
{
{
_mdt->DoActionFromBtoA();
}
{
return _sdt;
}
{
_sdt = sdt;
}
//Mediator.h
#define _MEDIATOR_H_
{
public:
virtual ~Mediator();
Mediator();
{
public:
ConcreteMediator();
Colleage* _clgA;
//Mediator.cpp
{
{
{
{
{
this->_clgA = clgA;
}
{
_clgB->SetState(_clgA->GetState());
}
{
this->_clgA = clgA;
}
{
this->_clgB = clgB;
}
{
return _clgA;
}
{
return _clgB;
}
{
this->_clgA = clgA;
}
{
_clgA->SetState(_clgB->GetState());
}
//main.cpp
using namespace std;
{
ConcreteMediator* m = new ConcreteMediator();
ConcreteColleageB* c2 = new ConcreteColleageB(m);
c2->SetState("old");
c1->Aciton();
c2->Aciton();
cout<<endl;
c1->Aciton();
c2->Aciton();
cout<<endl;
c2->Aciton();
c1->Aciton();
}
2.3.2 代码说明
Mediator模式的实现关键就是将对象Colleague之间的通信封装到一个类种单独处理,为了模拟Mediator模式的功能,这里给每个Colleague对象一个string型别以记录其状态,并通过状态改变来演示对象之间的交互和通信。这里主要就Mediator的示例运行结果给出分析:
1)将ConcreteColleageA对象设置状态“old”,ConcreteColleageB也设置状态“old”;
2)ConcreteColleageA对象改变状态,并在Action中和ConcreteColleageB对象进行通信,并改变ConcreteColleageB对象的状态为“new”;
3)ConcreteColleageB对象改变状态,并在Action中和ConcreteColleageA对象进行通信,并改变ConcreteColleageA对象的状态为“new”;
注意到,两个Colleague对象并不知道它交互的对象,并且也不是显示地处理交互过程,这一切都是通过Mediator对象完成的,示例程序运行的结果也正是证明了这一点。
2.4 讨论
Mediator模式是一种很有用并且很常用的模式,它通过将对象间的通信封装到一个类中,将多对多的通信转化为一对多的通信,降低了系统的复杂性。Mediator还获得系统解耦的特性,通过Mediator,各个Colleague就不必维护各自通信的对象和通信协议,降低了系统的耦合性,Mediator和各个Colleague就可以相互独立地修改了。
Mediator模式还有一个很显著额特点就是将控制集中,集中的优点就是便于管理,也正式符合了OO设计中的每个类的职责要单一和集中的原则。
- [设计模式]Mediator模式
- 设计模式(19)-中介者模式(Mediator)
- c++设计模式(19)-Mediator模式
- 设计模式之Mediator
- 设计模式之 mediator
- Mediator-----设计模式
- JAVA设计模式-Mediator
- 设计模式笔记-Mediator
- 设计模式之mediator模式
- 设计模式之Mediator模式
- 图解设计模式- Mediator 模式
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
- 设计模式(c++)笔记之十八(Mediator模式)
- 设计模式学习16 Mediator
- 笔记:Gof设计模式--Mediator
- 【设计模式】之十Mediator
- Mediator(中介者)设计模式
- 设计模式(十三)mediator
- 简单的聊天小软件
- 使用xerces-c++库编写XSD校验XML的类
- 俄罗斯方块源码
- 编译时_RTC_Shutdown和_RTC_InitBase相关错误的解决方法
- Reconstructing Position From Depth(从深度缓冲里重建位置信息)
- c++设计模式(19)-Mediator模式
- 在家怎么连接到公司的网络,ssh ,ftp ,3389 ...
- 11.3输入一个字符串,自动分配内存,复制
- istream_iterator和ostream_iterator的简单使用
- mentor EE2007.9安装及注册说明
- ubuntu下nutch-1.0的安装和配置错误排除
- c++设计模式(20)-Interpreter模式
- httpModules 与httpHandler
- zoj 2520