1、中介者模式

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


2、具体实现

(1)、代码如下

#include<string>usingnamespacestd;classMediator{public:virtualvoidgetParent()=0;private:};classcontreMediator{public:private:};classPerson{public:Person(stringname,intsex,intcondi){m_name=name;m_sex=sex;m_condi=condi;}stringgetName(){returnm_name;}intgetSex(){returnm_sex;}intgetCondi(){returnm_condi;}virtualvoidgetParent(Person*p)=0;protected:stringm_name;intm_sex;intm_condi;};////////////////////////////////////////////////////////////////////////////////////////////classWoman:publicPerson{public:Woman(stringname,intsex,intcondi):Person(name,sex,condi){}voidgetParent(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:};//////////////////////////////////////////////////////////////////////////////////////////////////classMan:publicPerson{public:Man(stringname,intsex,intcondi):Person(name,sex,condi){}voidgetParent(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:};intmain(void){Person*xiaofang=newWoman("小芳",2,5);Person*zhangsan=newMan("张三",1,4);Person*lisi=newMan("李四",1,5);xiaofang->getParent(zhangsan);xiaofang->getParent(lisi);return0;}

(2)、运行结果


3、命令模式

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


4、具体实现

(1)代码如下

#include<iostream>usingnamespacestd;classDoctor{public:voidtreatEye(){cout<<"医生治疗眼病"<<endl;}voidtreatNose(){cout<<"医生治疗鼻科病"<<endl;}private:};classCommand{public:virtualvoidtreat()=0;private:};classCommandTreatEye:publicCommand{public:CommandTreatEye(Doctor*doctor){m_doctor=doctor;}voidtreat(){m_doctor->treatEye();}private:Doctor*m_doctor;};classCommandTreatNose:publicCommand{public:CommandTreatNose(Doctor*doctor){m_doctor=doctor;}voidtreat(){m_doctor->treatNose();}private:Doctor*m_doctor;};classBeautyNurse{public:BeautyNurse(Command*command){this->command=command;}public:voidSubmittedCase(){//提交病类,下单命令command->treat();}private:Command*command;};intmain(void){/*//1、医生直接看病Doctor*doctor=newDoctor;doctor->treatEye();deletedoctor;*//*//2、通过一个命令Doctor*doctor=newDoctor;Command*command=newCommandTreatEye(doctor);command->treat();deletecommand;deletedoctor;*///护士提交简历,医生看病;Doctor*doctor=newDoctor;//Command*command=newCommandTreatEye(doctor);Command*command01=newCommandTreatNose(doctor);BeautyNurse*beautyNurse=newBeautyNurse(command01);beautyNurse->SubmittedCase();deletedoctor;deletecommand01;deletebeautyNurse;return0;}

(2)、运行结果