您好,登錄后才能下訂單哦!
1、中介者模式
就是借用一個中間的類,來完成其他2個類之間要實現(xiàn)的功能!!!
2、具體實現(xiàn)
(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)、運行結(jié)果
3、命令模式
把一個動作進行分解,分成發(fā)布者和接受者;
4、具體實現(xiàn)
(1)代碼如下
#include<iostream> using namespace std; class Doctor{ public: void treatEye(){ cout<<"醫(yī)生 治療 眼病"<<endl; } void treatNose(){ cout<<"醫(yī)生 治療 鼻科病"<<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、醫(yī)生直接看病 Doctor *doctor = new Doctor; doctor->treatEye(); delete doctor; */ /* //2、通過一個命令 Doctor *doctor = new Doctor; Command *command = new CommandTreatEye(doctor); command->treat(); delete command; delete doctor; */ //護士提交簡歷,醫(yī)生看病; 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)、運行結(jié)果
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。