zoukankan      html  css  js  c++  java
  • 中介者模式 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;
    }
    


     

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

  • 相关阅读:
    浅析Linux计算机进程地址空间与内核装载ELF
    ExtAspNet学习利用AppBox框架快速创建项目(四)vs2010解决方案
    ExtAspNet学习利用AppBox框架快速创建项目(五)—完成项目含源代码
    ExtAspNet学习利用AppBox框架快速创建项目(一)
    新手第一帖——学习.NET
    java hibernate4 学习心得
    ExtAspNet学习利用AppBox框架快速创建项目(三)Subsonic工具配置
    ExtAspNet学习利用AppBox框架快速创建项目(二)创建Oralce数据库
    我的编程疑团java还是.net
    修改.htaccess实现301重定向
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3149480.html
Copyright © 2011-2022 走看看