// 支持可撤销的操作; [8/14/2016 ZOSH];
// 模式特点:一、建立命令队列;二、可以将命令记入日志;三、接收请求的一方可以拒绝;四、添加一个新命令类不影响其它类;
// 烤肉串者 class Barbecuer { public: Barbecuer(void); ~Barbecuer(void); void BakeMutton() { cout<<"烤羊肉串"<<endl; } void BakeChickenWing() { cout<<"烤鸡翅"<<endl; } };
// 抽象命令; class Command { public: Command(void); virtual ~Command(void); Command(Barbecuer *pBarbecuer) { m_pBarbecuer = pBarbecuer; } virtual void ExcuteCommand() = 0; protected: Barbecuer *m_pBarbecuer; };
#include "Command.h" #include "Barbecuer.h" // 烤羊肉串命令; class BakeMuttonCommand : public Command { public: BakeMuttonCommand(void); ~BakeMuttonCommand(void); BakeMuttonCommand(Barbecuer *pBarbecuer) : Command(pBarbecuer) { } void ExcuteCommand() { if (m_pBarbecuer) { m_pBarbecuer->BakeMutton(); } } };
#include "Command.h" #include "Barbecuer.h" // 烤鸡翅命令; class BakeChickenWingCommand : public Command { public: BakeChickenWingCommand(void); ~BakeChickenWingCommand(void); BakeChickenWingCommand(Barbecuer *pBarbecuer) : Command(pBarbecuer) { } void ExcuteCommand() { if (m_pBarbecuer) { m_pBarbecuer->BakeMutton(); } } };