Chinaunix首页 | 论坛 | 博客
  • 博客访问: 169337
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 23:21:58




  1. #include <iostream>

  2. using namespace std;

  3. class Mediator;

  4. class Colleague {
  5. protected:
  6.     Mediator *mediator;

  7. public:
  8.     Colleague(Mediator *mediator) {
  9.         this->mediator = mediator;
  10.     }
  11. };

  12. class Mediator {
  13. public:
  14.     virtual void Send(string message, Colleague *colleague) = 0;
  15. };

  16. class ConcreteColleague1 : public Colleague {
  17. public:
  18.     ConcreteColleague1(Mediator *mediator):Colleague(mediator) {}
  19.     void Send(string message) {
  20.         message += " (Colleague1)";
  21.         mediator->Send(message,this);
  22.     }
  23.     void Notify(string message) {
  24.         cout << "Colleague1 get message: " << message << endl;
  25.     }
  26. };

  27. class ConcreteColleague2 : public Colleague {
  28. public:
  29.     ConcreteColleague2(Mediator *mediator):Colleague(mediator) {}
  30.     void Send(string message) {
  31.         message += " (Colleague2)";
  32.         mediator->Send(message,this);
  33.     }
  34.     void Notify(string message) {
  35.         cout << "Colleague2 get message: " << message << endl;;
  36.     }
  37. };

  38. class ConcreteColleague3 : public Colleague {
  39. public:
  40.     ConcreteColleague3(Mediator *mediator):Colleague(mediator) {}
  41.     void Send(string message) {
  42.         message += " (Colleague3)";
  43.         mediator->Send(message,this);
  44.     }
  45.     void Notify(string message) {
  46.         cout << "Colleague3 get message: " << message << endl;;
  47.     }
  48. };

  49. class ConcreteMeditor : public Mediator {
  50. public:
  51.     ConcreteMeditor() {}
  52.     void set(ConcreteColleague1 *colleague1,
  53.         ConcreteColleague2 *colleague2,
  54.         ConcreteColleague3 *colleague3) {
  55.         this->colleague1 = colleague1;
  56.         this->colleague2 = colleague2;
  57.         this->colleague3 = colleague3;
  58.     }
  59.     void Send(string message, Colleague *colleague) {
  60.         if(colleague == colleague1)
  61.         {
  62.             colleague2->Notify(message);
  63.             colleague3->Notify(message);
  64.         } else if (colleague == colleague2) {
  65.             colleague1->Notify(message);
  66.             colleague3->Notify(message);
  67.         }
  68.         else if (colleague == colleague3) {
  69.             colleague1->Notify(message);
  70.             colleague2->Notify(message);
  71.         }
  72.     }
  73. private:
  74.     ConcreteColleague1 *colleague1;
  75.     ConcreteColleague2 *colleague2;
  76.     ConcreteColleague3 *colleague3;
  77. };


  78. int main(int argc, char **argv)
  79. {
  80.     ConcreteMeditor *m = new ConcreteMeditor();
  81.     ConcreteColleague1 *c1 = new ConcreteColleague1(m);
  82.     ConcreteColleague2 *c2 = new ConcreteColleague2(m);
  83.     ConcreteColleague3 *c3 = new ConcreteColleague3(m);
  84.     m->set(c1, c2, c3);

  85.     c1->Send("Do you have lunch?");
  86.     c2->Send("No.");
  87.     c3->Send("Yes, I do.");
  88. }

阅读(1172) | 评论(0) | 转发(0) |
0

上一篇:访问者模式

下一篇:MVC模式

给主人留下些什么吧!~~