码迷,mamicode.com
首页 > 编程语言 > 详细

设计模式——中介者模式/调停者模式(C++实现)

时间:2017-05-24 23:59:54      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:set   ace   notify   namespace   images   design   virtual   中介者模式   c++实现   

技术分享

 

  1 #include <iostream>
  2 #include <string>
  3 
  4 using namespace std;
  5 
  6 class Colleague;
  7 
  8 class Mediator
  9 {
 10 public:
 11         virtual void Send(string strMessage, Colleague* pstColleague) = 0;
 12 };
 13 
 14 class Colleague
 15 {
 16 public:
 17         Colleague(Mediator* pstMediator): m_pstMediator(pstMediator)
 18         {
 19 
 20         }
 21 
 22         virtual void Send(string strMes) = 0;
 23 
 24         virtual void Notify(string strMes) = 0;
 25 
 26 protected:
 27         Mediator* m_pstMediator;
 28 };
 29 
 30 class ConcreteColleagueA: public Colleague
 31 {
 32 public:
 33         ConcreteColleagueA(Mediator* pstMediator):Colleague(pstMediator)
 34         {
 35 
 36         }
 37 
 38         virtual void Send(string strMsg)
 39         {
 40                 m_pstMediator->Send(strMsg, this);
 41 
 42         }
 43 
 44         virtual void Notify(string strMes)
 45         {
 46                 cout<< "同事1得到消息: "<< strMes<< endl;
 47         }
 48 };
 49 
 50 class ConcreteColleagueB: public Colleague
 51 {
 52 public:
 53         ConcreteColleagueB(Mediator* pstMediator):Colleague(pstMediator)
 54         {
 55 
 56         }
 57 
 58         virtual void Send(string strMsg)
 59         {
 60                 m_pstMediator->Send(strMsg, this);
 61 
 62         }
 63 
 64         virtual void Notify(string strMes)
 65         {
 66                 cout<< "同事2得到消息: "<< strMes<< endl;
 67         }
 68 };
 69 
 70 class ConcreteMediator: public Mediator
 71 {
 72 public:
 73         void SetColleagueA(ConcreteColleagueA* pstColleague)
 74         {
 75                 m_pstColleagueA = pstColleague;
 76         }
 77 
 78         void SetColleagueB(ConcreteColleagueB* pstColleague)
 79         {
 80                 m_pstColleagueB = pstColleague;
 81         }
 82 
 83         virtual void Send(string strMes, Colleague* pstColleague)
 84         {
 85                 if (pstColleague == m_pstColleagueA)
 86                 {
 87                         m_pstColleagueB->Notify(strMes);
 88                 }
 89                 else
 90                 {
 91                         m_pstColleagueA->Notify(strMes);
 92                 }
 93         }
 94 
 95 private:
 96         ConcreteColleagueA* m_pstColleagueA;
 97         ConcreteColleagueB* m_pstColleagueB;
 98 };
 99 
100 int main(int argc, char* argv[])
101 {
102         ConcreteMediator* pstMediatorA = new ConcreteMediator();
103 
104         ConcreteColleagueA* pstColleagueA = new ConcreteColleagueA(pstMediatorA);
105         ConcreteColleagueB* pstColleagueB = new ConcreteColleagueB(pstMediatorA);
106 
107         pstMediatorA->SetColleagueA(pstColleagueA);
108         pstMediatorA->SetColleagueB(pstColleagueB);
109 
110 
111         pstColleagueA->Send("How are you?");
112         pstColleagueB->Send("I am fine, Thank You!");
113 
114         return 0;
115 }
116 ////////////////////////////////////////////
117 [root@ ~/learn_code/design_pattern/21_mediator]$ ./mediator       
118 同事2得到消息: How are you?
119 同事1得到消息: I am fine, Thank You!

 

设计模式——中介者模式/调停者模式(C++实现)

标签:set   ace   notify   namespace   images   design   virtual   中介者模式   c++实现   

原文地址:http://www.cnblogs.com/070412-zwc/p/6901434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!