diff --git a/15.CommandPattern/2.Code/CommandPattern.h b/15.CommandPattern/2.Code/CommandPattern.h index 5e2b24c..dd13069 100644 --- a/15.CommandPattern/2.Code/CommandPattern.h +++ b/15.CommandPattern/2.Code/CommandPattern.h @@ -5,22 +5,22 @@ #include using namespace std; -// +// 命令队列类 #define COMMAND_QUEUE -// Command +// 抽象命令类 Command class Command { public: Command(){} virtual ~Command(){} - // ӿڣ + // 声明抽象接口:发送命令 virtual void execute() = 0; private: Command *command; }; -// ߣ +// 接收者:电灯类 class Lamp { public : @@ -42,7 +42,7 @@ private: bool lampState; }; -// ߣ +// 接收者:风扇类 class Fan { public: @@ -64,15 +64,19 @@ private: bool fanState; }; -// LampCommand +// 具体命令类 LampCommand class LampCommand :public Command { public: LampCommand(){ - printf("ؿƵ\n"); + printf("开关控制电灯\n"); lamp = new Lamp(); } - // ʵexecute() + ~LampCommand(){ + delete lamp; + lamp = nullptr; + } + // 实现execute() void execute(){ if (lamp->getLampState()){ lamp->off(); @@ -85,15 +89,19 @@ private: Lamp *lamp; }; -// FanCommand +// 具体命令类 FanCommand class FanCommand :public Command { public: FanCommand(){ - printf("ؿƷ\n"); + printf("开关控制风扇\n"); fan = new Fan(); } - // ʵexecute() + ~FanCommand(){ + delete lamp; + lamp = nullptr; + } + // 实现execute() void execute(){ if (fan->getFanState()){ fan->off(); @@ -106,18 +114,18 @@ private: Fan *fan; }; -// Button +// 调用者 Button class Button { public: Button(){} - // ע + // 注入具体命令类对象 void setCommand(Command *cmd){ this->command = cmd; } - // ť + // 发送命令:触摸按钮 void touch(){ - printf(":"); + printf("触摸开关:"); command->execute(); } private: @@ -126,10 +134,10 @@ private: #ifdef COMMAND_QUEUE /*************************************/ -/* */ +/* 命令队列 */ #include -// +// 命令队列类 class CommandQueue { public: @@ -149,18 +157,18 @@ private: }; -// +// 调用者 class Button2 { public: Button2(){} - // ע + // 注入具体命令队列类对象 void setCommandQueue(CommandQueue *cmdQueue){ this->cmdQueue = cmdQueue; } - // ť + // 发送命令:触摸按钮 void touch(){ - printf(":"); + printf("触摸开关:"); cmdQueue->execute(); } private: @@ -170,4 +178,4 @@ private: #endif -#endif //__COMMAND_PATTERN_H__ \ No newline at end of file +#endif //__COMMAND_PATTERN_H__