中介者模式 C++ 實現

#include<iostream>
#include<string>
#include<vector>
#include<cstdlib>

using namespace std;
/*
  mediator 模式
  問題:重在理解,結構圖見網絡。 
*/
class colleage
{
  private:
        string name;
        string content;
  public:
        colleage(string n = " "):name(n)
        {}
        void set_name(string name)
        {
            this->name = name;
        }      
        
        string get_name()
        {
          return this->name;    
        }
        
        void set_content(string content)
        {
              this->content = content;
        }
        
        string get_content()
        {
          return this->content;    
        }
        
        virtual void talk()
        {
            
        }
};

class monitor: public colleage
{
  public: 
  monitor(string n = " "):colleage(n)
  {
        
  }
   
  void talk()
  {
    cout <<"monitor says:"<<get_content()<< endl;      
  }
      
};

class tuanzishu: public colleage
{  
   public:
   tuanzishu(string n = " "):colleage(n)
   {
          
   }  
   
   void talk()
   {
     cout <<"tuanzishu say:"<<get_content()<< endl;     
   }  
    
};

class studentA: public colleage
{ public:
  studentA(string n = " "):colleage(n)
  {
          
  }
    
  void talk()
  {
    cout <<"studentA say:"<< get_content()<< endl;      
  }
    
};

class studentB: public colleage
{ public:
  studentB(string n = " "):colleage(n)
  {
        
  }
  
  void talk()
  {
    cout <<"studentB say:"<<get_content()<< endl;      
  }    
    
};

class mediator
{
  public:
        vector<colleage*> studentlist;
        virtual void add_students(colleage *student)
        {
          studentlist.push_back(student);    
        }
                   
};

class QQmediator: public mediator
{
  public:
      virtual void notify(colleage *student)
      {
            student->talk();
            for(int i = 0; i < studentlist.size(); i++)
            {
               if(student != studentlist[i])
                  {
                     studentlist[i]->talk();     
                }
             }
      }      
      virtual void chart(colleage *student1, colleage *student2)
      {
        student1->talk();
        student2->talk();      
      }
           
    
};

int main()
{//初始化 
  QQmediator qq;
  monitor studentmonitor("banzhang");
  tuanzishu studenttuanzishu("tuanzishu");
  studentA studentXM("xiaoming");
  studentB studentXH("xiaohong");
  
  //向中介者中添加同學 
  
  qq.add_students(&studentmonitor);
  qq.add_students(&studenttuanzishu);
  qq.add_students(&studentXM);
  qq.add_students(&studentXH);
  
  //設置各位同學的回覆 
  
  studentmonitor.set_content("班會現在開始,請大家積極發言。");
  studenttuanzishu.set_content("班會主題是XXXXX");
  studentXM.set_content("我認爲XXXXX");
  studentXH.set_content("我不認爲XXXX");
  
  //班長通知後開始 
  
  cout <<"班長髮佈一個通知後的情景:"<< endl;
  qq.notify(&studentmonitor); 
  
  //同學私聊
  
  cout <<"同學私聊的情景:"<< endl; 
  studentXM.set_content("你覺得今天的班會怎麼樣?");
  studentXH.set_content("一般般吧,還是老樣子,沒解決什麼問題");
  qq.chart(&studentXM,&studentXH);
  
  system("pause"); 
  return 0;
}


 

總結: 無。詳見參考:點擊打開鏈接

 

 

發佈了53 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章