文章目录
一、什么是命令模式
命令模式是一种行为设计模式,它将请求封装成一个对象,从而使你可以用不同的请求对客户端进行参数化。这种模式的主要目的是将方法的调用、请求或者操作封装到单一的对象中,然后通过调用该对象来实现对这些方法、请求或者操作的参数化和传递。
二、命令模式的实现原理
-
定义命令接口:创建一个命令接口,其中包含执行命令的方法。
-
实现具体命令类:创建具体的命令类,实现命令接口,并在其中实现具体的命令逻辑。
-
创建命令调用者:创建一个命令调用者类,它包含一个命令对象作为成员变量,并提供一个方法来设置命令对象。
-
调用者执行命令:调用者通过调用命令对象的执行方法来执行具体的命令。
-
创建命令接收者:创建一个命令接收者类,它包含了实际执行命令的方法。
-
将命令与接收者关联:将命令对象与接收者对象进行关联,使得命令对象可以调用接收者的方法来执行具体的操作。
三、命令模式的应用场景
- 撤销和重做操作:命令模式可以记录每个操作的命令对象,从而可以轻松地实现撤销和重做功能。
- 队列请求处理:命令模式可以将请求封装成命令对象,并将其放入队列中进行处理,从而实现异步、延迟或者顺序执行请求。
- 日志记录:命令模式可以记录每个操作的命令对象,从而可以方便地实现日志记录功能。
- 菜单或按钮操作:命令模式可以将菜单项或按钮操作封装成命令对象,从而实现灵活的菜单或按钮操作。
四、命令模式的代码实现
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);
}