首页 > 代码库 > Mediator 模式

Mediator 模式

在面向对象系统的设计和开发过程中,对象之间的交互和通信是最为常见的情况,因为对象间的交互本身就是一种通信。在系统比较小的时候,可能对象间的通信不是很多、对象也比较少,我们可以直接硬编码到各个对象的方法中。但是当系统规模变大,对象的量变引起系统复杂度的急剧增加,对象间的通信也变得越来越复杂,这时候我们就要提供一个专门处理对象间交互和通信的类,这个中介者就是 Mediator 模式

 1 //////////Colleage.h////////////////////////////////////////////////////////////////
 2 #pragma once
 3 #include <string>
 4 using namespace std;
 5 class Mediator ;
 6 class Colleage
 7 {
 8 public:
 9     virtual ~Colleage();
10     virtual void Action() = 0 ;
11     virtual void SetState(const string& sdt) = 0 ;
12     virtual string GetState() = 0 ;
13 
14 protected:
15     Colleage();
16     Colleage(Mediator* mdt);
17     Mediator* _mdt ;
18 
19 private:
20 };
21 
22 class ConcreteColleageA : public Colleage
23 {
24 public:
25     ~ConcreteColleageA();
26     ConcreteColleageA();
27     ConcreteColleageA(Mediator* mdt);
28     void Action();
29     void SetState(const string& sdt);
30     string GetState();
31 
32 protected:
33 private:
34     string _sdt ;
35 };
36 
37 class ConcreteColleageB : public Colleage
38 {
39 public:
40     ~ConcreteColleageB();
41     ConcreteColleageB();
42     ConcreteColleageB(Mediator* mdt);
43     void Action();
44     void SetState(const string& sdt);
45     string GetState();
46 
47 protected:
48 private:
49     string _sdt ;
50 };
 1 ///////////Mediator.h///////////////////////////////////////////////////////////////
 2 #pragma once
 3 class Colleage;
 4 class Mediator
 5 {
 6 public:
 7     virtual ~Mediator();
 8     virtual void DoActionFromAtoB() = 0 ;
 9     virtual void DoActionFromBtoA() = 0 ;
10 protected:
11     Mediator();
12 private:
13 };
14 
15 class ConcreteMediator : public Mediator
16 {
17 public:
18     ~ConcreteMediator();
19     ConcreteMediator();
20     ConcreteMediator(Colleage* clgA ,Colleage* clgB);
21 
22     void SetColleageA( Colleage* clgA );
23     void SetColleageB( Colleage* clgB );
24 
25     Colleage* GetColleageA();
26     Colleage* GetColleageB();
27 
28     void IntroColleage(Colleage* clgA ,Colleage* clgB);
29 
30     void DoActionFromAtoB();
31     void DoActionFromBtoA();
32 protected:
33 private:
34     Colleage* _clgA ;
35     Colleage* _clgB ;
36 };
 1 ////////////Colleage.cpp//////////////////////////////////////////////////////////////
 2 #include "Colleage.h"
 3 #include "Mediator.h"
 4 #include <string>
 5 #include <iostream>
 6 using namespace std;
 7 Colleage::~Colleage()
 8 {
 9 
10 }
11 Colleage::Colleage()
12 {
13 
14 }
15 Colleage::Colleage(Mediator* mdt)
16 {
17     this->_mdt = mdt ;
18 }
19 
20 ConcreteColleageA::~ConcreteColleageA()
21 {
22 
23 }
24 ConcreteColleageA::ConcreteColleageA()
25 {
26 
27 }
28 ConcreteColleageA::ConcreteColleageA(Mediator* mdt):Colleage(mdt)
29 {
30 
31 }
32 void ConcreteColleageA::SetState(const string& sdt)
33 {
34     this->_sdt = sdt ;
35 }
36 string ConcreteColleageA::GetState()
37 {
38     return _sdt ;
39 }
40 
41 void ConcreteColleageA::Action()
42 {
43     this->_mdt->DoActionFromAtoB();
44     cout<<"把消息:"<<this->GetState()<<"传给B"<<endl;
45 }
46 
47 ConcreteColleageB::~ConcreteColleageB()
48 {
49 
50 }
51 ConcreteColleageB::ConcreteColleageB()
52 {
53 
54 }
55 ConcreteColleageB::ConcreteColleageB(Mediator* mdt):Colleage(mdt)
56 {
57 
58 }
59 void ConcreteColleageB::SetState(const string& sdt)
60 {
61     this->_sdt = sdt ;
62 }
63 string ConcreteColleageB::GetState()
64 {
65     return _sdt ;
66 }
67 
68 void ConcreteColleageB::Action()
69 {
70     this->_mdt->DoActionFromBtoA();
71     cout<<"把消息:"<<this->GetState()<<"传给A"<<endl;
72 }
 1 //////////Mediator.cpp////////////////////////////////////////////////////////////////
 2 #include "Mediator.h"
 3 #include "Colleage.h"
 4 Mediator::Mediator()
 5 {
 6 
 7 }
 8 Mediator::~Mediator()
 9 {
10 
11 }
12 
13 ConcreteMediator::ConcreteMediator()
14 {
15 
16 }
17 ConcreteMediator::~ConcreteMediator()
18 {
19 
20 }
21 ConcreteMediator::ConcreteMediator(Colleage* clgA ,Colleage* clgB)
22 {
23     _clgA = clgA ;
24     _clgB = clgB ;
25 }
26 void ConcreteMediator::DoActionFromAtoB()
27 {
28     _clgB->SetState(_clgA->GetState());
29 }
30 void ConcreteMediator::DoActionFromBtoA()
31 {
32     _clgA->SetState(_clgB->GetState());
33 }
34 
35 void ConcreteMediator::SetColleageA( Colleage* clgA )
36 {
37     _clgA = clgA ;
38 }
39 void ConcreteMediator::SetColleageB( Colleage* clgB )
40 {
41     _clgB = clgB ;
42 }
43 Colleage* ConcreteMediator::GetColleageA()
44 {
45     return _clgA ;
46 }
47 Colleage* ConcreteMediator::GetColleageB()
48 {
49     return _clgB ;
50 }
51 
52 void ConcreteMediator::IntroColleage(Colleage* clgA ,Colleage* clgB)
53 {
54     _clgA = clgA ;
55     _clgB = clgB ;
56 }
 1 //////main.cpp////////////////////////////////////////////////////////////////////
 2 #include "Colleage.h"
 3 #include "Mediator.h"
 4 #include <iostream>
 5 using namespace std;
 6 int main()
 7 {
 8     ConcreteMediator* m = new ConcreteMediator();
 9     Colleage* c1 = new ConcreteColleageA(m);
10     Colleage* c2 = new ConcreteColleageB(m);
11 
12     m->IntroColleage(c1, c2);
13     c1->SetState("你好!");
14     c2->SetState("你是谁");
15 
16     cout<<c1->GetState()<<endl;
17     cout<<c2->GetState()<<endl;
18 
19     c1->Action();
20 
21     cout<<c2->GetState()<<endl;
22     getchar();
23     return 0 ;
24 
25 }