• 设计模式---命令模式


    定义

    命令模式(Command Pattern):将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化;对请求排队或者记录请求日志,以及支持可撤销的操作。命令模式是一种对象行为型模式,其别名为动作(Action)模式或事务(Transaction)模式。

    模式结构

    命令模式包含如下角色:

    • Command: 抽象命令类
    • ConcreteCommand: 具体命令类
    • Invoker: 调用者
    • Receiver: 接收者
    • Client:客户类

    时序图

     

    代码

    #include <iostream>
    #include "ConcreteCommand.h"
    #include "Invoker.h"
    #include "Receiver.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        Receiver * pReceiver = new Receiver();
        ConcreteCommand * pCommand = new ConcreteCommand(pReceiver);
        Invoker * pInvoker = new Invoker(pCommand);
        pInvoker->call();
        
        delete pReceiver;
        delete pCommand;
        delete pInvoker;
        return 0;
    }
    ///////////////////////////////////////////////////////////
    //  Receiver.h
    //  Implementation of the Class Receiver
    //  Created on:      07-十月-2014 17:44:02
    //  Original author: colin
    ///////////////////////////////////////////////////////////
    
    #if !defined(EA_8E5430BB_0904_4a7d_9A3B_7169586237C8__INCLUDED_)
    #define EA_8E5430BB_0904_4a7d_9A3B_7169586237C8__INCLUDED_
    
    class Receiver
    {
    public:
        Receiver();
        virtual ~Receiver();
        void action();
    };
    #endif // !defined(EA_8E5430BB_0904_4a7d_9A3B_7169586237C8__INCLUDED_)
    ///////////////////////////////////////////////////////////
    //  Receiver.cpp
    //  Implementation of the Class Receiver
    //  Created on:      07-十月-2014 17:44:02
    //  Original author: colin
    ///////////////////////////////////////////////////////////
    
    #include "Receiver.h"
    #include <iostream>
    using namespace std;
    
    Receiver::Receiver(){
    }
    
    Receiver::~Receiver(){
    }
    
    void Receiver::action(){
        cout << "receiver action." << endl;
    }
    ///////////////////////////////////////////////////////////
    //  ConcreteCommand.h
    //  Implementation of the Class ConcreteCommand
    //  Created on:      07-十月-2014 17:44:01
    //  Original author: colin
    ///////////////////////////////////////////////////////////
    
    #if !defined(EA_1AE70D53_4868_4e81_A1B8_1088DA355C23__INCLUDED_)
    #define EA_1AE70D53_4868_4e81_A1B8_1088DA355C23__INCLUDED_
    
    #include "Command.h"
    #include "Receiver.h"
    
    class ConcreteCommand : public Command
    {
    
    public:
        ConcreteCommand(Receiver * pReceiver);
        virtual ~ConcreteCommand();
        virtual void execute();
    private:
        Receiver *m_pReceiver;
    };
    #endif // !defined(EA_1AE70D53_4868_4e81_A1B8_1088DA355C23__INCLUDED_)
    ///////////////////////////////////////////////////////////
    //  ConcreteCommand.cpp
    //  Implementation of the Class ConcreteCommand
    //  Created on:      07-十月-2014 17:44:02
    //  Original author: colin
    ///////////////////////////////////////////////////////////
    
    #include "ConcreteCommand.h"
    #include <iostream>
    using namespace std;
    
    
    ConcreteCommand::ConcreteCommand(Receiver *pReceiver){
        m_pReceiver = pReceiver;
    }
    
    ConcreteCommand::~ConcreteCommand(){
    
    }
    
    void ConcreteCommand::execute(){
        cout << "ConcreteCommand::execute"  << endl;
        m_pReceiver->action();
    }
    ///////////////////////////////////////////////////////////
    //  Invoker.h
    //  Implementation of the Class Invoker
    //  Created on:      07-十月-2014 17:44:02
    //  Original author: colin
    ///////////////////////////////////////////////////////////
    
    #if !defined(EA_3DACB62A_0813_4d11_8A82_10BF1FB00D9A__INCLUDED_)
    #define EA_3DACB62A_0813_4d11_8A82_10BF1FB00D9A__INCLUDED_
    
    #include "Command.h"
    
    class Invoker
    {
    
    public:
        Invoker(Command * pCommand);
        virtual ~Invoker();
        void call();
    
    private:
        Command *m_pCommand;
    
    
    };
    #endif // !defined(EA_3DACB62A_0813_4d11_8A82_10BF1FB00D9A__INCLUDED_)
    ///////////////////////////////////////////////////////////
    //  Invoker.cpp
    //  Implementation of the Class Invoker
    //  Created on:      07-十月-2014 17:44:02
    //  Original author: colin
    ///////////////////////////////////////////////////////////
    
    #include "Invoker.h"
    #include <iostream>
    using namespace std;
    
    Invoker::Invoker(Command * pCommand){
        m_pCommand = pCommand;
    }
    
    Invoker::~Invoker(){
    
    }
    
    void Invoker::call(){
        cout << "invoker calling" << endl;
        m_pCommand->execute();
    }
  • 相关阅读:
    RMAN备份脚本
    配置putty隧道登录远程window远程或远程数据库
    ssh登录报错:no common kex alg
    Solaris上修改进程能够打开的最大文件句柄数
    如何在V890上安装配置rsc(转)
    Oracle表或分区导出脚本
    检查日期合法性脚本(转)
    Solaris下常用的磁带操作命令
    如何从solaris操作系统上获取机器的sn号
    如何修复failed磁盘和"DISABLED RECOVER"状态的plex
  • 原文地址:https://www.cnblogs.com/zhanghu52030/p/9402152.html
Copyright © 2020-2023  润新知