知识点
命令(command)、动作(action)、事务(Transaction)
知识内容
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
示例
#include<iostream>
using namespace std;
class Receiver
{
public:
Receiver() {}
void Action()
{
cout << "exec action in Receiver..." << endl;
}
};
class Command
{
public:
virtual void Execute() = 0;
};
class ConcreteCommand:public Command
{
private:
Receiver receiver;
public:
ConcreteCommand(Receiver myReceiver):
receiver(myReceiver) {}
virtual void Execute()
{
cout << "exec command in ConcreteCommand" << endl;
receiver.Action();
}
};
int main()
{
Receiver* receiver = new Receiver();
Command* command = new ConcreteCommand(*receiver);
command -> Execute();
return 0;
}
链接
https://github.com/xierensong/learngit/blob/master/DPattern/21/1.cpp