中介者模式 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;

}


 

总结: 无。详见参考:点击打开链接

 

 

 

你可能感兴趣的:(中介者模式)