新版MQL语言程序设计:命令模式的原理、应用及代码实现

文章目录

一、什么是命令模式

命令模式是一种行为设计模式,它将请求封装成一个对象,从而使你可以用不同的请求对客户端进行参数化。这种模式的主要目的是将方法的调用、请求或者操作封装到单一的对象中,然后通过调用该对象来实现对这些方法、请求或者操作的参数化和传递。

二、命令模式的实现原理

  1. 定义命令接口:创建一个命令接口,其中包含执行命令的方法。

  2. 实现具体命令类:创建具体的命令类,实现命令接口,并在其中实现具体的命令逻辑。

  3. 创建命令调用者:创建一个命令调用者类,它包含一个命令对象作为成员变量,并提供一个方法来设置命令对象。

  4. 调用者执行命令:调用者通过调用命令对象的执行方法来执行具体的命令。

  5. 创建命令接收者:创建一个命令接收者类,它包含了实际执行命令的方法。

  6. 将命令与接收者关联:将命令对象与接收者对象进行关联,使得命令对象可以调用接收者的方法来执行具体的操作。

三、命令模式的应用场景

  • 撤销和重做操作:命令模式可以记录每个操作的命令对象,从而可以轻松地实现撤销和重做功能。
  • 队列请求处理:命令模式可以将请求封装成命令对象,并将其放入队列中进行处理,从而实现异步、延迟或者顺序执行请求。
  • 日志记录:命令模式可以记录每个操作的命令对象,从而可以方便地实现日志记录功能。
  • 菜单或按钮操作:命令模式可以将菜单项或按钮操作封装成命令对象,从而实现灵活的菜单或按钮操作。

四、命令模式的代码实现

cpp 复制代码
//+------------------------------------------------------------------+
//| interface --- for patterns                                         |
//+------------------------------------------------------------------+
interface ClientInterface //pattern client
{
    string Output(void); //returns header
    void Run(void); //execute the pattern client
};
//+------------------------------------------------------------------+
//| interface --- for patterns                                         |
//+------------------------------------------------------------------+
void Run(ClientInterface* client) //launches a pattern
{
    printf("---\n%s",client.Output()); //print pattern header
    client.Run(); //execute client collaborations
    delete client; //exit
}

//+------------------------------------------------------------------+
//| participants > receiver                                          |
//+------------------------------------------------------------------+
class Receiver
//   knows how to perform the request operations
//   any class may be a receiver
{
    public:
        Receiver(void);
        Receiver(Receiver&);
        void              Action(void);
};
//+------------------------------------------------------------------+
//| participants > receiver > default                                |
//+------------------------------------------------------------------+
Receiver::Receiver(void)
{

}
//+------------------------------------------------------------------+
//| participants > receiver > copy constructor                       |
//+------------------------------------------------------------------+
Receiver::Receiver(Receiver &src)
{

}
//+------------------------------------------------------------------+
//| participants > receiver > action                                 |
//+------------------------------------------------------------------+
void Receiver::Action(void)
{
    Print("receiver ",&this," action");
}
//+------------------------------------------------------------------+
//| participants > command                                           |
//+------------------------------------------------------------------+
class Command
{
    protected:
        Receiver*         m_receiver;
    public:
        Command(Receiver*);
        ~Command(void);
        virtual void      Execute(void)=0;
};
//+------------------------------------------------------------------+
//| participants > command > constructor                             |
//+------------------------------------------------------------------+
Command::Command(Receiver* receiver)
{
    m_receiver = new Receiver(receiver);
    Print("receiver ",receiver," accepted by command ",&this);
}
//+------------------------------------------------------------------+
//| participants > command > destructor                              |
//+------------------------------------------------------------------+
Command::~Command(void)
{
    if(CheckPointer(m_receiver)==1)
    {
        delete m_receiver;
    }
}
//+------------------------------------------------------------------+
//| participants > concrete command                                  |
//+------------------------------------------------------------------+
class ConcreteCommand:public Command
{
    protected:
        int               m_state;
    public:
        ConcreteCommand(Receiver*);
        void              Execute(void);
};
//+------------------------------------------------------------------+
//| participants > concrete command > constructor                    |
//+------------------------------------------------------------------+
ConcreteCommand::ConcreteCommand(Receiver* receiver): Command(receiver), m_state(0)
{
    Print("command ", &this, " state: ", m_state);
}
//+------------------------------------------------------------------+
//| participants > concrete command > execute                        |
//+------------------------------------------------------------------+
void ConcreteCommand::Execute(void)
{
    Print("command executes receiver ", m_receiver);
    m_receiver.Action();
    m_state = 1;
    Print("command ", &this, " state: ", m_state);
}
// 要求命令执行请求
class Invoker
{
    public:
        ~Invoker();
        void              StoreCommand(Command*);
        void              Execute(void);
    protected:
        Command          *m_command;
};
//+------------------------------------------------------------------+
//| participants > invoker > destructor                              |
//+------------------------------------------------------------------+
Invoker::~Invoker()
{
    if (CheckPointer(m_command) == 1)
    {
        delete m_command;
    }
}
//+------------------------------------------------------------------+
//| participants > invoker > command                                 |
//+------------------------------------------------------------------+
void Invoker::StoreCommand(Command* command)
{
    m_command = command;
    Print("command ", m_command, " stored");
}
//+------------------------------------------------------------------+
//| participants > invoker > execute                                 |
//+------------------------------------------------------------------+
void Invoker::Execute(void)
{
    Print("executing command ",m_command);
    m_command.Execute();
}
 // creates a concrete command object and sets its receiver
class Client:public ClientInterface
{
    public:
        string            Output(void);
        void              Run(void);
};
string Client::Output(void)
{
    return __FUNCTION__;
}
//+------------------------------------------------------------------+
//| collaborations                                                   |
//+------------------------------------------------------------------+
void Client::Run(void)
{
    Receiver receiver;
    Invoker invoker;
    invoker.StoreCommand(new ConcreteCommand(&receiver));
    invoker.Execute();
}
//
void OnStart()
{
    Run(new Client);
}
相关推荐
Red Red38 分钟前
网安基础知识|IDS入侵检测系统|IPS入侵防御系统|堡垒机|VPN|EDR|CC防御|云安全-VDC/VPC|安全服务
网络·笔记·学习·安全·web安全
贰十六1 小时前
笔记:Centos Nginx Jdk Mysql OpenOffce KkFile Minio安装部署
笔记·nginx·centos
知兀1 小时前
Java的方法、基本和引用数据类型
java·笔记·黑马程序员
Natural_yz2 小时前
大数据学习17之Spark-Core
大数据·学习·spark
qq_172805592 小时前
RUST学习教程-安装教程
开发语言·学习·rust·安装
一只小小汤圆3 小时前
opencascade源码学习之BRepOffsetAPI包 -BRepOffsetAPI_DraftAngle
c++·学习·opencascade
醉陌离3 小时前
渗透测试笔记——shodan(4)
笔记
虾球xz3 小时前
游戏引擎学习第20天
前端·学习·游戏引擎
LateBloomer7773 小时前
FreeRTOS——信号量
笔记·stm32·学习·freertos
legend_jz3 小时前
【Linux】线程控制
linux·服务器·开发语言·c++·笔记·学习·学习方法