资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

中介者模式和命令模式

1、中介者模式

公司主营业务:网站设计、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出船营免费做网站回馈大家。

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

2、具体实现

  (1)、代码如下

#include
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<<"不是×××"<getCondi() == p->getCondi()){
                cout<getName()<<"和"<getName()<<"绝配"<getName()<<"和"<getName()<<"不配"<m_sex == p->getSex()){
                cout<<"不是×××"<getCondi() == p->getCondi()){
                cout<getName()<<"和"<getName()<<"绝配"<getName()<<"和"<getName()<<"不配"<getParent(zhangsan);
    xiaofang->getParent(lisi);


    return 0;
}

  (2)、运行结果

中介者模式和命令模式

3、命令模式

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


4、具体实现

  (1)代码如下

#include
using namespace std;

class Doctor{
    public:
        void treatEye(){
            cout<<"医生 治疗 眼病"<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)、运行结果

中介者模式和命令模式


网站题目:中介者模式和命令模式
分享地址:http://cdkjz.cn/article/jjgeoc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220