设计模式-命令模式

    xiaoxiao2025-04-26  5

    //   命令模式:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及

    //   支持可撤销的操作; [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(); } } };

    转载请注明原文地址: https://ju.6miu.com/read-1298469.html
    最新回复(0)