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)、运行结果
名称栏目:中介者模式和命令模式
本文URL:https://www.cdcxhl.com/article2/jjgeoc.html
成都网站建设公司_创新互联,为您提供响应式网站、网站营销、用户体验、营销型网站建设、手机网站建设、软件开发
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联