標籤:中介者模式 命令模式
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)、運行結果
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/8C/07/wKiom1hfLRSxD2JqAABGN6dj1Y8487.png-wh_500x0-wm_3-wmp_4-s_3954700717.png" title="QQ20161225102057.png" alt="wKiom1hfLRSxD2JqAABGN6dj1Y8487.png-wh_50" />
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)、運行結果
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/8C/03/wKioL1hfLomBtoRxAAA1jfC3dp4028.png-wh_500x0-wm_3-wmp_4-s_3648325578.png" title="QQ20161225102704.png" alt="wKioL1hfLomBtoRxAAA1jfC3dp4028.png-wh_50" />
本文出自 “wait0804” 部落格,請務必保留此出處http://wait0804.blog.51cto.com/11586096/1885903
中介者模式和命令模式