diff --git a/Command-Pattern/Command.hpp b/Command-Pattern/Command.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b09bb5a04a02bf8438fbeb6c611d71a1b7b1fa88 --- /dev/null +++ b/Command-Pattern/Command.hpp @@ -0,0 +1,11 @@ +#ifndef COMMAND_HPP +#define COMMAND_HPP + +class Command { +public: + virtual void execute() = 0; + + virtual ~Command() = default; +}; + +#endif \ No newline at end of file diff --git a/Command-Pattern/GarageDoorOpen.hpp b/Command-Pattern/GarageDoorOpen.hpp new file mode 100644 index 0000000000000000000000000000000000000000..334ff5db47673f490dd4a0921b1c086b4911a2db --- /dev/null +++ b/Command-Pattern/GarageDoorOpen.hpp @@ -0,0 +1,20 @@ +#ifndef GARAGEDOOROPEN_HPP +#define GARAGEDOOROPEN_HPP + +#include +#include + +#include "Command.hpp" + +class GarageDoorOpen : public Command { +private: + std::string door; +public: + GarageDoorOpen(std::string g) : door(g) {} + + void execute() { + std::cout << door << " was opened" << std::endl; + } +}; + +#endif \ No newline at end of file diff --git a/Command-Pattern/LightOnCommand.hpp b/Command-Pattern/LightOnCommand.hpp new file mode 100644 index 0000000000000000000000000000000000000000..cb5ac144e3ec88492ecd82804d79650b5b83a2bc --- /dev/null +++ b/Command-Pattern/LightOnCommand.hpp @@ -0,0 +1,20 @@ +#ifndef LIGHTONCOMMAND_HPP +#define LIGHTONCOMMAND_HPP + +#include +#include + +#include "Command.hpp" + +class LightOnCommand : public Command { +private: + std::string light; +public: + LightOnCommand(std::string l) : light(l) {} + + void execute() { + std::cout << light << " was turned on" << std::endl; + } +}; + +#endif \ No newline at end of file diff --git a/Command-Pattern/Makefile b/Command-Pattern/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..9ce53e4916c61b476d2d1c709ca95af247321ae7 --- /dev/null +++ b/Command-Pattern/Makefile @@ -0,0 +1,5 @@ +all: + g++ -g -Wall main.cpp -o main + +clean: + rm main diff --git a/Command-Pattern/README.md b/Command-Pattern/README.md new file mode 100644 index 0000000000000000000000000000000000000000..20cc13285a0871f24b624168a2cc14ddc7b505d2 --- /dev/null +++ b/Command-Pattern/README.md @@ -0,0 +1,3 @@ +## 命令模式 + +![](https://img-blog.csdnimg.cn/20190616212348873.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlc3Ricm9va2xpdQ==,size_16,color_FFFFFF,t_70) \ No newline at end of file diff --git a/Command-Pattern/SimpleRemoteControl.hpp b/Command-Pattern/SimpleRemoteControl.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ca78c53c63f6e57a6c0b22f382a377f913a98b43 --- /dev/null +++ b/Command-Pattern/SimpleRemoteControl.hpp @@ -0,0 +1,20 @@ +#ifndef SIMPLEREMOTECONTROL_HPP +#define SIMPLEREMOTECONTROL_HPP + +#include "Command.hpp" + +class SimpleRemoteControl { +private: + Command *slot; + +public: + void setCommand(Command *command) { + slot = command; + } + + void buttonWasPressed() { + slot->execute(); + } +}; + +#endif \ No newline at end of file diff --git a/Command-Pattern/main.cpp b/Command-Pattern/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18c2ffd020d3b76ff5ad1291775382375061e81c --- /dev/null +++ b/Command-Pattern/main.cpp @@ -0,0 +1,19 @@ +#include "LightOnCommand.hpp" +#include "SimpleRemoteControl.hpp" +#include "GarageDoorOpen.hpp" + +int main() { + SimpleRemoteControl *remote = new SimpleRemoteControl(); + LightOnCommand *lightOn = new LightOnCommand("my light"); + + remote->setCommand(lightOn); + remote->buttonWasPressed(); + + GarageDoorOpen *doorOpen = new GarageDoorOpen("my garage door"); + remote->setCommand(doorOpen); + remote->buttonWasPressed(); + + delete remote; + delete lightOn; + delete doorOpen; +} \ No newline at end of file diff --git a/README.md b/README.md index 56e119fdc4282067f556aeac1f9de289b994bda3..ebfee976578d98f1d05e7098ff1e3429eaab6f98 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ + [Chain of Resposibility(职责链)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Chain%20of%20Resposibility) ### 行为变化: + [Command](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Command) ++ [Command(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Command-Pattern) + [Visitor](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Visitor) ### 领域问题: + [Interpreter](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Interpreter)