From 30640e9bc52cb50507175f8d3780f11b6a70c896 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Thu, 4 Nov 2021 22:47:40 +0300 Subject: [PATCH] lesson_6 --- README.md | 3 +++ lesson_6/Application.hpp | 19 +++++++++++++++++++ lesson_6/Command.hpp | 15 +++++++++++++++ lesson_6/Control.cpp | 19 +++++++++++++++++++ lesson_6/Control.hpp | 19 +++++++++++++++++++ lesson_6/Document.hpp | 32 ++++++++++++++++++++++++++++++++ lesson_6/OpenCommand.cpp | 31 +++++++++++++++++++++++++++++++ lesson_6/OpenCommand.hpp | 24 ++++++++++++++++++++++++ lesson_6/PasteCommand.cpp | 17 +++++++++++++++++ lesson_6/PasteCommand.hpp | 20 ++++++++++++++++++++ lesson_6/SimpleCommand.cpp | 15 +++++++++++++++ lesson_6/SimpleCommand.hpp | 24 ++++++++++++++++++++++++ lesson_6/main.cpp | 22 ++++++++++++++++++++++ 13 files changed, 260 insertions(+) create mode 100644 lesson_6/Application.hpp create mode 100644 lesson_6/Command.hpp create mode 100644 lesson_6/Control.cpp create mode 100644 lesson_6/Control.hpp create mode 100644 lesson_6/Document.hpp create mode 100644 lesson_6/OpenCommand.cpp create mode 100644 lesson_6/OpenCommand.hpp create mode 100644 lesson_6/PasteCommand.cpp create mode 100644 lesson_6/PasteCommand.hpp create mode 100644 lesson_6/SimpleCommand.cpp create mode 100644 lesson_6/SimpleCommand.hpp create mode 100644 lesson_6/main.cpp diff --git a/README.md b/README.md index e69de29..3bac352 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,3 @@ +Паттерн "Команда" инкапсулирует запрос в виде объекта, делая возможной параметризацию клиентский объектов с другими запросами, организацию очереди или регистрацию запросов, а также поддержку отмены операций. + +Паттерн "Команда" отделяет объект, выдающий запросы, от объекта, который умеет эти запросы выполнять. Объект команды инкапсулирует получателя с операцией (или набором операций). Инициатор вызывает метод *execute()* объекта команды, что приводит к выполнению соответствующих операций с получателем. Возможна параметризация инициаторов командами. Команды могут поддерживать механизмы отмены, восстанавливающий объект в состояние до последнего вызова метода *execute()*. Макрокоманды - простое расширение паттерна "Команда", позволяющее выполнять цепочки из нескольких команд. \ No newline at end of file diff --git a/lesson_6/Application.hpp b/lesson_6/Application.hpp new file mode 100644 index 0000000..51d3125 --- /dev/null +++ b/lesson_6/Application.hpp @@ -0,0 +1,19 @@ +/* + * Application.hpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Document.hpp" + +class Application +{ +public: + void Add(Document *doc) + { + std::cout << "Документ " << doc->getName() << " был добавлен!" << std::endl; + } +}; diff --git a/lesson_6/Command.hpp b/lesson_6/Command.hpp new file mode 100644 index 0000000..398556d --- /dev/null +++ b/lesson_6/Command.hpp @@ -0,0 +1,15 @@ +/* + * Command.hpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +class Command +{ +public: + virtual void Execute() = 0; + virtual ~Command() {}; +}; diff --git a/lesson_6/Control.cpp b/lesson_6/Control.cpp new file mode 100644 index 0000000..a5ed55b --- /dev/null +++ b/lesson_6/Control.cpp @@ -0,0 +1,19 @@ +/* + * Control.cpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#include "Control.hpp" + +void Control::setCommand(Command* command) +{ + _command = command; +} + +void Control::run() +{ + _command->Execute(); +} + diff --git a/lesson_6/Control.hpp b/lesson_6/Control.hpp new file mode 100644 index 0000000..aba095f --- /dev/null +++ b/lesson_6/Control.hpp @@ -0,0 +1,19 @@ +/* + * Control.hpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Command.hpp" + +class Control +{ +private: + Command* _command; +public: + void setCommand(Command*); + void run(); +}; diff --git a/lesson_6/Document.hpp b/lesson_6/Document.hpp new file mode 100644 index 0000000..127bae5 --- /dev/null +++ b/lesson_6/Document.hpp @@ -0,0 +1,32 @@ +/* + * Document.hpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +class Document +{ +private: + std::string _name; +public: + Document(const std::string name) : _name(name) + { + } + void Open() + { + std::cout << "Документ " << _name << " открыт!" << std::endl; + } + void Paste() + { + std::cout << "Вставлено в документ " << _name << std::endl; + } + std::string getName() const + { + return _name; + } +}; diff --git a/lesson_6/OpenCommand.cpp b/lesson_6/OpenCommand.cpp new file mode 100644 index 0000000..0ab0c5e --- /dev/null +++ b/lesson_6/OpenCommand.cpp @@ -0,0 +1,31 @@ +/* + * OpenCommand.cpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#include "OpenCommand.hpp" +#include "Document.hpp" + +OpenCommand::OpenCommand(Application *a) : _application(a) +{ +} + +void OpenCommand::Execute() +{ + std::string name = AskUser(); + if (!name.empty()) + { + Document *document = new Document(name); + _application->Add(document); + document->Open(); + } +} + +std::string OpenCommand::AskUser() +{ + std::string name; + std::cin >> name; + return name; +} diff --git a/lesson_6/OpenCommand.hpp b/lesson_6/OpenCommand.hpp new file mode 100644 index 0000000..7427a61 --- /dev/null +++ b/lesson_6/OpenCommand.hpp @@ -0,0 +1,24 @@ +/* + * OpenCommand.hpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Command.hpp" +#include "Application.hpp" +#include + +class OpenCommand: public Command +{ +private: + Application *_application; + std::string _response; +protected: + virtual std::string AskUser(); +public: + OpenCommand(Application*); + virtual void Execute(); +}; diff --git a/lesson_6/PasteCommand.cpp b/lesson_6/PasteCommand.cpp new file mode 100644 index 0000000..62f0d92 --- /dev/null +++ b/lesson_6/PasteCommand.cpp @@ -0,0 +1,17 @@ +/* + * PasteCommand.cpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#include "PasteCommand.hpp" + +PasteCommand::PasteCommand(Document *doc) : _document(doc) +{ +} + +void PasteCommand::Execute() +{ + _document->Paste(); +} diff --git a/lesson_6/PasteCommand.hpp b/lesson_6/PasteCommand.hpp new file mode 100644 index 0000000..ca69ea1 --- /dev/null +++ b/lesson_6/PasteCommand.hpp @@ -0,0 +1,20 @@ +/* + * PasteCommand.hpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Command.hpp" +#include "Document.hpp" + +class PasteCommand: public Command +{ +private: + Document* _document; +public: + PasteCommand(Document*); + virtual void Execute(); +}; diff --git a/lesson_6/SimpleCommand.cpp b/lesson_6/SimpleCommand.cpp new file mode 100644 index 0000000..8f0749d --- /dev/null +++ b/lesson_6/SimpleCommand.cpp @@ -0,0 +1,15 @@ +/* + * SimpleCommand.cpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#include "SimpleCommand.hpp" + +template +void SimpleCommand::Execute() +{ + (_receiver->*_action)(); +} + diff --git a/lesson_6/SimpleCommand.hpp b/lesson_6/SimpleCommand.hpp new file mode 100644 index 0000000..d85d857 --- /dev/null +++ b/lesson_6/SimpleCommand.hpp @@ -0,0 +1,24 @@ +/* + * SimpleCommand.hpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Command.hpp" + +template +class SimpleCommand: public Command +{ +public: + typedef void (Receiver::*Action)(); + SimpleCommand(Receiver *r, Action a) : _receiver(r), _action(a) + { + } + virtual void Execute(); +private: + Action _action; + Receiver *_receiver; +}; diff --git a/lesson_6/main.cpp b/lesson_6/main.cpp new file mode 100644 index 0000000..59b105f --- /dev/null +++ b/lesson_6/main.cpp @@ -0,0 +1,22 @@ +/* + * main.cpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#include "Control.hpp" +#include "Application.hpp" +#include "OpenCommand.hpp" +#include "PasteCommand.hpp" + +int main() +{ + Control control; + Application app; + OpenCommand openCommand(&app); + control.setCommand(&openCommand); + control.run(); + + return 0; +}