设计模式(17)-行为型模式-Moderator模式

来源:互联网 发布:决对争锋网络剧资源 编辑:程序博客网 时间:2024/06/06 05:15

3.5           Moderator模式

3.5.1      功能

可以通过将集体行为封装在一个单独的mediator当中来避免不同组件(对象)之间的依赖关系。Mediator负责控制盒协调一组对象间的交互。Mediator是的组中的对象不再相互显式的调用。

3.5.2      结构


•  M e d i a t o r(中介者)
— 中介者定义一个接口用于与各同事(C o l l e a g u e)对象通信。

•  C o n c r e t e M e d i a t o r(具体中介者)
— 具体中介者通过协调各同事对象实现协作行为。
— 了解并维护它的各个同事。

•  Colleague class(同事类)

— 每一个同事类都知道它的中介者对象。

— 每一个同事对象在需与其他的同事通信的时候,与它的中介者通信。

• 同事向一个中介者对象发送和接收请求。中介者在各同事间适当地转发请求以实现协作行为。



3.5.3      C++代码

//Colleage.h

#ifndef _COLLEAGE_H_

#define _COLLEAGE_H_

#include<string>

using namespace std;

classMediator;

classColleage

{

public:

       virtual ~Colleage();

       virtual void Aciton() = 0;

       virtual voidSetState(conststring& sdt) = 0;

       virtual stringGetState() = 0;

protected:

       Colleage();

       Colleage(Mediator* mdt);

       Mediator* _mdt;

private:

};

 

class ConcreteColleageA :public Colleage

{

public:

       ConcreteColleageA();

       ConcreteColleageA(Mediator* mdt);

       ~ConcreteColleageA();

       void Aciton();

       void SetState(conststring& sdt);

       string GetState();

protected:

private:

       string _sdt;

};

 

classConcreteColleageB:public Colleage

{

public:

       ConcreteColleageB();

       ConcreteColleageB(Mediator* mdt);

       ~ConcreteColleageB();

       void Aciton();

       void SetState(const string& sdt);

       string GetState();

protected:

private:

       string _sdt;

};

#endif //~_COLLEAGE_H_

 

 

//Colleage.cpp

#include"Mediator.h"

#include"Colleage.h"

#include<iostream>

using namespace std;

Colleage::Colleage()

{}

Colleage::Colleage(Mediator*mdt)

{

       this->_mdt = mdt;

}

Colleage::~Colleage()

{}

ConcreteColleageA::ConcreteColleageA()

{}

ConcreteColleageA::~ConcreteColleageA()

{}

ConcreteColleageA::ConcreteColleageA(Media

       tor* mdt) :Colleage(mdt) { }

stringConcreteColleageA::GetState()

{

       return _sdt;

}

void ConcreteColleageA::SetState(const

       string& sdt)

{

       _sdt = sdt;

}

void ConcreteColleageA::Aciton()

{

       _mdt->DoActionFromAtoB();

       cout << "StateofConcreteColleageB:" <<"

              "<<this->GetState()<<endl;

}

ConcreteColleageB::ConcreteColleageB()

{}

ConcreteColleageB::~ConcreteColleageB()

{}

ConcreteColleageB::ConcreteColleageB(Media

       tor* mdt) :Colleage(mdt)

{}

void ConcreteColleageB::Aciton()

{

       _mdt->DoActionFromBtoA();

       cout << "State of ConcreteColleageB:" <<"

              "<<this->GetState()<<endl;

}

stringConcreteColleageB::GetState()

{

       return _sdt;

}

void ConcreteColleageB::SetState(const

       string& sdt)

{

       _sdt = sdt;

}

 

//Mediator.h

#ifndef _MEDIATOR_H_

#define _MEDIATOR_H_

classColleage;

classMediator

{

public:

       virtual ~Mediator();

       virtual voidDoActionFromAtoB() = 0;

       virtual voidDoActionFromBtoA() = 0;

protected:

       Mediator();

private:

};

class ConcreteMediator :public Mediator

{

public:

       ConcreteMediator();

       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* _clgA;

       Colleage* _clgB;

};

#endif //~_MEDIATOR_H

 

//Mediator.cpp

#include"Mediator.h"

#include"Colleage.h"

Mediator::Mediator()

{}

Mediator::~Mediator()

{}

ConcreteMediator::ConcreteMediator()

{}

ConcreteMediator::~ConcreteMediator()

{}

ConcreteMediator::ConcreteMediator(Colleage

       * clgA, Colleage*clgB)

{

       this->_clgA = clgA;

       this->_clgB = clgB;

}

void ConcreteMediator::DoActionFromAtoB()

{

       _clgB->SetState(_clgA->GetState());

}

void

ConcreteMediator::SetConcreteColleageA(Coll

eage*clgA)

{

       this->_clgA = clgA;

}

void

ConcreteMediator::SetConcreteColleageB(Coll

eage*clgB)

{

       this->_clgB = clgB;

}

Colleage*

ConcreteMediator::GetConcreteColleageA()

{

       return _clgA;

}

Colleage*

ConcreteMediator::GetConcreteColleageB()

{

       return _clgB;

}

 

void

ConcreteMediator::IntroColleage(Colleage*

clgA,Colleage* clgB)

{

       this->_clgA = clgA;

       this->_clgB = clgB;

}

void ConcreteMediator::DoActionFromBtoA()

{

       _clgA->SetState(_clgB->GetState());

}

 

//main.cpp

#include"Mediator.h"

#include"Colleage.h"

#include<iostream>

using namespace std;

int main(int argc, char*argv[])

{

       ConcreteMediator* m = new

              ConcreteMediator();

       ConcreteColleageA* c1 = new

              ConcreteColleageA(m);

       ConcreteColleageB* c2 = new

              ConcreteColleageB(m);

       m->IntroColleage(c1, c2);

       c1->SetState("old");

       c2->SetState("old");

       c1->Aciton();

       c2->Aciton();

       cout << endl;

       c1->SetState("new");

       c1->Aciton();

       c2->Aciton();

       cout << endl;

       c2->SetState("old");

       c2->Aciton();

       c1->Aciton();

       return 0;

}

 

0 0