<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      設(shè)計模式之二十一:中介者模式(Mediator)

      中介者模式:定義了一個對象。用來封裝一系列對象的交互。中介者模式通過使對象之間不必顯式引用減少了對象之間的耦合,而且同意你獨立改變它們之間的交互。

      中介者模式就是將對象之間的交互封裝在了一個獨立的對象中。這個獨立的對象用來控制對象之間的交互行為,所以這個對象還是蠻復(fù)雜的。

      UML類圖:
      這里寫圖片描寫敘述

      主要包含:

      1. Mediator:定義了一個Colleague對象之間交互的接口。
      2. ConcreteMediator:實現(xiàn)了Colleague對象之間的交互行為,并了解和能操作Colleague對象。
      3. Colleague classes:每個Colleague都認(rèn)識Mediator對象。而且通過Mediator來實現(xiàn)彼此之間的通訊。

      基礎(chǔ)C++代碼例如以下:

      #include <iostream>
      #include <string>
      
      using namespace std;
      
      class Colleague;
      class Mediator
      {
          public:
          virtual void send(string message,Colleague * c)=0;
      
      };
      
      
      class Colleague
      {
              public:
              Colleague(Mediator *m)
              {
                  mediator=m;
              }
              void send(string message)
              {
                  mediator->send(message,this);
              }
              virtual void notify(string message)=0;
              protected:
              Mediator * mediator;
      
      };
      
      class ConcreteColleague1:public Colleague
      {
              public:
              ConcreteColleague1(Mediator * c):Colleague(c)
              {
      
              }
              void notify(string message)
              {
                  cout<<"concreteColleague1 receive message:"<<message<<endl;
              }
      
      };
      
      class ConcreteColleague2:public Colleague
      {
              public:
              ConcreteColleague2(Mediator *m):Colleague(m)
              {
      
              }
              void notify(string message)
              {
                  cout<<"concreteColleague2 receive message:"<<message<<endl;
              }
      
      };
      
      
      class ConcreteMediator:public Mediator
      {
          public:
          void send(string message,Colleague * c)
          {
              ConcreteColleague1* c1=dynamic_cast<ConcreteColleague1 *>(c);
              ConcreteColleague2* c2=dynamic_cast<ConcreteColleague2 *>(c);
              if(c1!=NULL)
              {
                  cc2->notify(message);
              }
      
              if(c2!=NULL)
              {
                  cc1->notify(message);
              }
      
          }
      
          void setCC1(ConcreteColleague1 * c)
          {
              cc1=c;
          }   
          void setCC2(ConcreteColleague2 * c)
          {
              cc2=c;
          }
          private:
          ConcreteColleague1 * cc1;
          ConcreteColleague2 * cc2;
      
      };
      
      
      int main()
      {
          ConcreteMediator *m=new ConcreteMediator();
          ConcreteColleague1 * cc1=new ConcreteColleague1(m);
          ConcreteColleague2 * cc2=new ConcreteColleague2(m);
      
          m->setCC1(cc1);
          m->setCC2(cc2);
      
          cc1->send("how are you !");
          cc2->send("fine ,thank you");
      
          return 0;
      
      
      }
      

      運行輸出:
      這里寫圖片描寫敘述


      一個使用中介者模式的聊天室的樣例。

      這里實現(xiàn)了Mediator和Colleague的一對多的聚合關(guān)系。這種中介者模式才是有意義的使用方式。
      UML類圖:
      這里寫圖片描寫敘述

      C++代碼例如以下:

      #include <iostream>
      #include <map>
      #include <string>
      
      
      using namespace std;
      
      class Participant;
      
      class AbstractChatRoom
      {
              public:
                      virtual void registe(Participant * p)=0;
                      virtual void send(string from,string to ,string message)=0;
      };
      
      class ChatRoom:public AbstractChatRoom
      {
              public:
                      void registe(Participant * p);
      
                      void send(string from,string to ,string message);
              private:
                      map<Participant *,string> participants;
      
      };
      
      
      class Participant
      {
              public:
                      Participant(string n=""):name(n)
                      {
      
                      }
      
                      string getName()
                      {
                          return name;
                      }
                      void setChatRoom(ChatRoom *c)
                      {
                          chatRoom=c;
                      }
                      void send(string to,string message)
                      {
                          chatRoom->send(name,to,message);
                      }
      
                      virtual void receive(string from,string message)
                      {
                          cout<<from<<" to "<<name<<" : "<<message<<endl;
                      }
              private:
                      string name;
                      ChatRoom * chatRoom;
      
      };
      
      
      void ChatRoom::registe(Participant * p)
                      {
                          if(!participants.count(p))
                          {
                              participants.insert(pair<Participant *,string>(p,p->getName()));
                          }
                          p->setChatRoom(this);
                      }
      
      
                      void ChatRoom::send(string from,string to ,string message)
                      {
                          map<Participant *,string>::iterator iter;
                          for(iter=participants.begin();iter!=participants.end();iter++)
                          {
                              if(iter->second==to)
                                      break;
                          }
                          if(iter!=participants.end())
                          {
                              iter->first->receive(from,message);
                          }
                      }
      
      
      
      
      class Beatle:public Participant
      {
              public:
                      Beatle(string n=""):Participant(n)
                      {
      
                      }
                      void receive(string from,string message)
                      {
                          cout<<"to a beatle : ";
                          Participant::receive(from,message);
                      }
      
      };
      
      class NonBeatle:public Participant
      {
              public:
                      NonBeatle(string n=""):Participant(n)
                      {
      
                      }
                      void receive(string from,string message)
                      {
                          cout<<"to a non-beatle : ";
                          Participant::receive(from,message);
                      }
      
      };
      
      
      int main()
      {
          cout<<"聊天室中介者模式代碼"<<endl;
          ChatRoom * chatRoom=new ChatRoom();
      
          Participant *george=new Beatle("George");
          Participant *paul=new Beatle("Paul");
          Participant *ringo=new Beatle("Ringo");
          Participant *john=new Beatle("John");
          Participant *yoko=new NonBeatle("Yoko");
      
          chatRoom->registe(george);
          chatRoom->registe(paul);
          chatRoom->registe(ringo);
          chatRoom->registe(john);
          chatRoom->registe(yoko);
      
      
          yoko->send("John","hi John!");
          paul->send("Ringo","All you need is love");
          ringo->send("George","My sweet Lord");
          paul->send("John","can not buy me love");
          john->send("Yoko","My sweet love");
      
          return 0;
      }
      

      運行輸出:
      這里寫圖片描寫敘述

      posted on 2017-04-09 13:49  gcczhongduan  閱讀(282)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 久久伊99综合婷婷久久伊| 国产精品亚洲一区二区z| 日韩高清不卡一区二区三区| 国产精品久久久久久免费软件| 午夜免费无码福利视频麻豆| 精品尤物TV福利院在线网站| 日本一区二区精品色超碰| 国产午夜在线观看视频| 无码人妻丰满熟妇奶水区码| 亚洲国产成人AⅤ片在线观看| 国产jizzjizz视频| 亚洲大尺度一区二区三区| 久久综合国产色美利坚| 91中文字幕在线一区| 91午夜福利一区二区三区| 亚洲精品国产av成拍色拍个| 国产精品无遮挡又爽又黄| 午夜免费无码福利视频麻豆| 孙吴县| 中文字幕有码无码AV| 久久国产综合色免费观看| 欧美丰满熟妇bbbbbb| 欧美人成精品网站播放| 久久精品女人天堂av| 国产精品欧美福利久久| 亚洲尤码不卡av麻豆| 亚洲综合91社区精品福利| 精品无码国产污污污免费| 免费无码观看的AV在线播放| 湟中县| 爱性久久久久久久久| 亚洲人成网网址在线看| 久久99久久99精品免观看| 男人的天堂av社区在线| 国产日韩精品欧美一区灰| 青草成人精品视频在线看| 疯狂做受xxxx高潮欧美日本| 成人精品老熟妇一区二区| 中文字幕国产日韩精品| 精品国产成人国产在线视| 中文乱码字幕在线中文乱码|