中介者模式和命令模式
1、中介者模式
公司主营业务:网站设计、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出船营免费做网站回馈大家。
就是借用一个中间的类,来完成其他2个类之间要实现的功能!!!
2、具体实现
(1)、代码如下
#includeusing 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<<"不是×××"< getCondi() == p->getCondi()){ cout< getName()<<"和"< getName()<<"绝配"< getName()<<"和"< getName()<<"不配"< m_sex == p->getSex()){ cout<<"不是×××"< getCondi() == p->getCondi()){ cout< getName()<<"和"< getName()<<"绝配"< getName()<<"和"< getName()<<"不配"< getParent(zhangsan); xiaofang->getParent(lisi); return 0; }
(2)、运行结果
3、命令模式
把一个动作进行分解,分成发布者和接受者;
4、具体实现
(1)代码如下
#includeusing namespace std; class Doctor{ public: void treatEye(){ cout<<"医生 治疗 眼病"< 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:http://myzitong.com/article/jjgeoc.html