首页 > 代码库 > 中介者模式和命令模式
中介者模式和命令模式
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)、运行结果
本文出自 “wait0804” 博客,请务必保留此出处http://wait0804.blog.51cto.com/11586096/1885903
中介者模式和命令模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。