意图:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
class Receiver { public:Receiver(){}virtual ~Receiver(){}virtual void Action(){cout<<"Receiver::Action()"<<endl;} };class Command { public:Command(){}virtual ~Command(){}virtual void Execute() = 0; };class ConcreteCommand: public Command { public:ConcreteCommand(Receiver* receiver){this->receiver = receiver;}~ConcreteCommand(){}void Execute(){cout<<"ConcreteCommand::Execute()"<<endl;receiver->Action();}private:Receiver* receiver; };class Invoker { public:Invoker(Command* cmd){this->cmd = cmd;}~Invoker(){}void Invoke(){cmd->Execute();}private:Command* cmd; };