zoukankan      html  css  js  c++  java
  • 设计模式---命令模式

    定义

    命令模式(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();
    }
  • 相关阅读:
    Shell学习(四)Shell运算符
    Shell学习(三)Shell参数传递
    Shell学习(二)Shell变量
    Shell学习(一)认识Shell
    JVM学习(四)JVM调优
    JVM学习(三)JVM垃圾回收
    JVM学习(二)JVM加载类
    JVM学习(一)什么是JVM
    Python学习————包
    Python学习————模块
  • 原文地址:https://www.cnblogs.com/zhanghu52030/p/9402152.html
Copyright © 2011-2022 走看看