diff --git a/15.CommandPattern/1.Picture/命令模式.jpg b/15.CommandPattern/1.Picture/命令模式.jpg new file mode 100644 index 0000000..c13283b Binary files /dev/null and b/15.CommandPattern/1.Picture/命令模式.jpg differ diff --git a/15.CommandPattern/1.Picture/命令模式UML图.png b/15.CommandPattern/1.Picture/命令模式UML图.png new file mode 100644 index 0000000..1108167 Binary files /dev/null and b/15.CommandPattern/1.Picture/命令模式UML图.png differ diff --git a/15.CommandPattern/1.Picture/命令模式实例UML图.png b/15.CommandPattern/1.Picture/命令模式实例UML图.png new file mode 100644 index 0000000..5ad1c67 Binary files /dev/null and b/15.CommandPattern/1.Picture/命令模式实例UML图.png differ diff --git a/15.CommandPattern/2.Code/CommandPattern.h b/15.CommandPattern/2.Code/CommandPattern.h new file mode 100644 index 0000000..b0fbe50 --- /dev/null +++ b/15.CommandPattern/2.Code/CommandPattern.h @@ -0,0 +1,172 @@ +#ifndef __COMMAND_PATTERN_H__ +#define __COMMAND_PATTERN_H__ + +#include +#include +using namespace std; + +// +#define COMMAND_QUEUE + +// Command +class Command +{ +public: + Command(){} + // ӿڣ + virtual void execute() = 0; +private: + Command *command; +}; + +// ߣ +class Lamp +{ +public : + Lamp(){ + this->lampState = false; + } + void on(){ + lampState = true; + printf("Lamp is on\n"); + } + void off(){ + lampState = false; + printf("Lamp is off\n"); + } + bool getLampState(){ + return lampState; + } +private: + bool lampState; +}; + +// ߣ +class Fan +{ +public: + Fan(){ + this->fanState = false; + } + void on(){ + fanState = true; + printf("Fan is on\n"); + } + void off(){ + fanState = false; + printf("Fan is off\n"); + } + bool getFanState(){ + return fanState; + } +private: + bool fanState; +}; + +// LampCommand +class LampCommand :public Command +{ +public: + LampCommand(){ + printf("ؿƵ\n"); + lamp = new Lamp(); + } + // ʵexecute() + void execute(){ + if (lamp->getLampState()){ + lamp->off(); + } + else{ + lamp->on(); + } + } +private: + Lamp *lamp; +}; + +// FanCommand +class FanCommand :public Command +{ +public: + FanCommand(){ + printf("ؿƷ\n"); + fan = new Fan(); + } + // ʵexecute() + void execute(){ + if (fan->getFanState()){ + fan->off(); + } + else{ + fan->on(); + } + } +private: + Fan *fan; +}; + +// Button +class Button +{ +public: + Button(){} + // ע + void setCommand(Command *cmd){ + this->command = cmd; + } + // ť + void touch(){ + printf(":"); + command->execute(); + } +private: + Command *command; +}; + +#ifdef COMMAND_QUEUE +/*************************************/ +/* */ +#include + +// +class CommandQueue +{ +public: + CommandQueue(){ + } + void addCommand(Command *cmd){ + commandQueue.push_back(cmd); + } + void execute(){ + for (int i = 0; i < commandQueue.size(); i++) + { + commandQueue[i]->execute(); + } + } +private: + vectorcommandQueue; + +}; + +// +class Button2 +{ +public: + Button2(){} + // ע + void setCommandQueue(CommandQueue *cmdQueue){ + this->cmdQueue = cmdQueue; + } + // ť + void touch(){ + printf(":"); + cmdQueue->execute(); + } +private: + CommandQueue *cmdQueue; +}; + +#endif + + +#endif //__COMMAND_PATTERN_H__ \ No newline at end of file diff --git a/15.CommandPattern/2.Code/main.cpp b/15.CommandPattern/2.Code/main.cpp new file mode 100644 index 0000000..7482798 --- /dev/null +++ b/15.CommandPattern/2.Code/main.cpp @@ -0,0 +1,49 @@ +#include +#include "CommandPattern.h" + +int main() +{ + // ʵߣť + Button *button = new Button(); + Command *lampCmd, *fanCmd; + + // ťƵ + lampCmd = new LampCommand(); + button->setCommand(lampCmd); + button->touch(); + button->touch(); + button->touch(); + + printf("\n\n"); + + // ťƷ + fanCmd = new FanCommand(); + button->setCommand(fanCmd); + button->touch(); + button->touch(); + button->touch(); + +#ifdef COMMAND_QUEUE + + printf("\n\n***********************************\n"); + Button2 *button2 = new Button2(); + Command *lampCmd2, *fanCmd2; + CommandQueue *cmdQueue = new CommandQueue(); + + // ťƵ + lampCmd2 = new LampCommand(); + cmdQueue->addCommand(lampCmd2); + + // ťƷ + fanCmd2 = new FanCommand(); + cmdQueue->addCommand(fanCmd2); + + button2->setCommandQueue(cmdQueue); + button2->touch(); + +#endif + + printf("\n\n"); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index 3ff1046..04e1dbb 100644 --- a/README.md +++ b/README.md @@ -63,10 +63,14 @@ Jungle设计模式系列 博客地址:https://blog.csdn.net/sinat_21107433/article/details/102763849 -15.设计模式(十六)——代理模式 +16.设计模式(十六)——代理模式 博客地址:https://blog.csdn.net/sinat_21107433/article/details/102772697 -16.设计模式(十七)——职责链模式 +17.设计模式(十七)——职责链模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102790445 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102790445 + +18.设计模式(十八)——命令模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102810123 \ No newline at end of file