默默地EEer,原文地址:
http://www.cnblogs.com/hebaichuanyeah/p/5713031.html
意图
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
一个栗子
#include <iostream> using namespace std; class Receiver { public : void doSomeThing() { cout<<"执行命令"<<endl; } }; class Command { public: virtual ~Command(){} virtual void execute() { } }; class ADemoCommand:public Command { private: Receiver * receiver; public: ADemoCommand(Receiver * r) { receiver = r; } virtual void execute() { receiver->doSomeThing(); } }; class Invoker { private: Command * command; public: Invoker(Command *c) { command=c; } void sendCommand() { command->execute(); } }; main() { Receiver * receiver = new Receiver(); Command * command = new ADemoCommand(receiver); Invoker * invoker = new Invoker(command); invoker->sendCommand(); delete receiver; delete command; delete invoker; }