1、中介者模式
公司主營業(yè)務(wù):網(wǎng)站設(shè)計、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出船營免費做網(wǎng)站回饋大家。
就是借用一個中間的類,來完成其他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、命令模式
把一個動作進(jìn)行分解,分成發(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; */ //護(hù)士提交簡歷,醫(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é)果
名稱欄目:中介者模式和命令模式
本文URL:http://aaarwkj.com/article2/jjgeoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站營銷、用戶體驗、營銷型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)