1. 程式人生 > >設計模式(17)-行為型模式-Moderator模式

設計模式(17)-行為型模式-Moderator模式

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(const string& 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(const string& 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;

}