温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

中介者模式和命令模式

发布时间:2020-07-11 13:51:48 来源:网络 阅读:942 作者:汇天下豪杰 栏目:编程语言

1、中介者模式

  就是借用一个中间的类,来完成其他2个类之间要实现的功能!!!


2、具体实现

  (1)、代码如下

#include<string> using namespace std; class Mediator{     public:         virtual void getParent() = 0;     private: }; class contreMediator{     public:     private: };    class Person{     public:         Person(string name, int sex, int condi){             m_name = name;             m_sex = sex;             m_condi = condi;         }         string getName(){             return m_name;         }         int getSex(){             return m_sex;         }         int getCondi(){             return m_condi;         }         virtual void getParent(Person *p) = 0;     protected:         string m_name;         int m_sex;         int m_condi; }; //////////////////////////////////////////////////////////////////////////////////////////// class Woman : public Person{     public:         Woman(string name, int sex, int condi) : Person(name, sex, condi){         }         void getParent(Person *p){             if(this->m_sex == p->getSex()){                 cout<<"不是×××"<<endl;                }             if(this->getCondi() == p->getCondi()){                 cout<<this->getName()<<"和"<<p->getName()<<"绝配"<<endl;             }else{                 cout<<this->getName()<<"和"<<p->getName()<<"不配"<<endl;             }         }     private: }; ////////////////////////////////////////////////////////////////////////////////////////////////// class Man : public Person{     public:         Man(string name, int sex, int condi) : Person(name, sex, condi){         }         void getParent(Person *p){             if(this->m_sex == p->getSex()){                 cout<<"不是×××"<<endl;             }             if(this->getCondi() == p->getCondi()){                 cout<<this->getName()<<"和"<<p->getName()<<"绝配"<<endl;             }else{                 cout<<this->getName()<<"和"<<p->getName()<<"不配"<<endl;             }              }     private: }; int main(void){     Person *xiaofang = new Woman("小芳", 2, 5);     Person *zhangsan = new Man("张三", 1, 4);     Person *lisi = new Man("李四", 1, 5);     xiaofang->getParent(zhangsan);     xiaofang->getParent(lisi);     return 0; }

  (2)、运行结果

中介者模式和命令模式


3、命令模式

 把一个动作进行分解,分成发布者和接受者;


4、具体实现

  (1)代码如下

#include<iostream> using namespace std; class Doctor{     public:         void treatEye(){             cout<<"医生 治疗 眼病"<<endl;         }            void treatNose(){             cout<<"医生 治疗 鼻科病"<<endl;         }        private: }; class Command{     public:         virtual void treat() = 0;     private: }; class CommandTreatEye : public Command{     public:         CommandTreatEye(Doctor *doctor){             m_doctor = doctor;         }         void treat(){             m_doctor->treatEye();         }     private:         Doctor *m_doctor; }; class CommandTreatNose : public Command{     public:         CommandTreatNose(Doctor *doctor){             m_doctor = doctor;         }         void treat(){             m_doctor->treatNose();         }     private:         Doctor *m_doctor; }; class BeautyNurse{     public:         BeautyNurse(Command *command){             this->command = command;         }     public:             void SubmittedCase(){  //提交病类, 下单命令             command->treat();         }     private:         Command *command; }; int main(void){     /*     //1、医生直接看病     Doctor *doctor = new Doctor;     doctor->treatEye();     delete doctor;     */     /*     //2、通过一个命令     Doctor *doctor = new Doctor;     Command *command = new CommandTreatEye(doctor);     command->treat();     delete command;     delete doctor;     */     //护士提交简历,医生看病;     Doctor *doctor = new Doctor; //  Command *command = new CommandTreatEye(doctor);     Command *command01 = new CommandTreatNose(doctor);     BeautyNurse *beautyNurse = new BeautyNurse(command01);     beautyNurse->SubmittedCase();     delete doctor;     delete command01;     delete beautyNurse;     return 0; }

  (2)、运行结果

中介者模式和命令模式







向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI