中介者模式和命令模式

來源:互聯網
上載者:User

標籤:中介者模式   命令模式   

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

中介者模式和命令模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.