接口隔离

踏踏实实做事

导航

命令模式

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Command        //命令
{
public:
    virtual void execute() = 0;
};


class Invoker        //调用者
{
private:
    Command * cmd;
public:
    void setCommand(Command * mCmd)
    {
        this->cmd = mCmd;
    }
    void action()
    {
        this->cmd->execute();
    }
};

class Receiver        //接受者
{
public:
    void doSomething()
    {
        cout<<"接受者业务处理"<<endl;
    }
    
};

class ConCommand: public Command
{
private:
    Receiver * receiver;
public:
    ConCommand(Receiver * receiver)
    {
        this->receiver = receiver;
    }
    void execute()
    {
        this->receiver->doSomething();
    }
};

void main()
{
    Receiver * receiver = new Receiver();
    Command  * cmd      = new ConCommand(receiver);
    cmd->execute();
    
    //客户端执行命令
    //客户端通过调用者来执行命令  
    Invoker * invoker = new Invoker();  
    invoker->setCommand(cmd);  
    invoker->action();  
}

posted on 2016-10-28 13:31  接口隔离  阅读(66)  评论(0编辑  收藏  举报