Type: Behavioral
命令模式将请求包装成Object,这样能够是clients接收不同的请求作为参数,支持undo操作。例子里undo没实现。
#include <iostream>
#include <vector>
using namespace std;
class Receiver
{
public:
void action()
{
cout <<"Receiver Action"<<endl;;
}
};
class Command
{
public:
virtual void execute()=0;
};
class ConcreteCommand: public Command
{
public:
ConcreteCommand(Receiver* pReceiver): m_pReceiver(pReceiver){};
void execute()
{
m_pReceiver->action();
};
private:
Receiver* m_pReceiver;
};
class Invoker
{
public:
void addCommand(Command *pCommand)
{
mCommand.push_back(pCommand);
pCommand->execute();
}
private:
vector<Command*> mCommand;
};
int main()
{
Receiver* pReceiver = new Receiver();
Command* pCommand = new ConcreteCommand(pReceiver);
Invoker* pInvoker = new Invoker();
pInvoker->addCommand(pCommand);
system("pause");
return 0;
}
命令模式将请求包装成Object,这样能够是clients接收不同的请求作为参数,支持undo操作。例子里undo没实现。