This commit is contained in:
Alexander Zhirov 2021-11-04 22:47:40 +03:00
parent 3b15979dfe
commit 30640e9bc5
13 changed files with 260 additions and 0 deletions

View File

@ -0,0 +1,3 @@
Паттерн "Команда" инкапсулирует запрос в виде объекта, делая возможной параметризацию клиентский объектов с другими запросами, организацию очереди или регистрацию запросов, а также поддержку отмены операций.
Паттерн "Команда" отделяет объект, выдающий запросы, от объекта, который умеет эти запросы выполнять. Объект команды инкапсулирует получателя с операцией (или набором операций). Инициатор вызывает метод *execute()* объекта команды, что приводит к выполнению соответствующих операций с получателем. Возможна параметризация инициаторов командами. Команды могут поддерживать механизмы отмены, восстанавливающий объект в состояние до последнего вызова метода *execute()*. Макрокоманды - простое расширение паттерна "Команда", позволяющее выполнять цепочки из нескольких команд.

19
lesson_6/Application.hpp Normal file
View File

@ -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;
}
};

15
lesson_6/Command.hpp Normal file
View File

@ -0,0 +1,15 @@
/*
* Command.hpp
*
* Created on: 4 нояб. 2021 г.
* Author: alexander
*/
#pragma once
class Command
{
public:
virtual void Execute() = 0;
virtual ~Command() {};
};

19
lesson_6/Control.cpp Normal file
View File

@ -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();
}

19
lesson_6/Control.hpp Normal file
View File

@ -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();
};

32
lesson_6/Document.hpp Normal file
View File

@ -0,0 +1,32 @@
/*
* Document.hpp
*
* Created on: 4 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
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;
}
};

31
lesson_6/OpenCommand.cpp Normal file
View File

@ -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;
}

24
lesson_6/OpenCommand.hpp Normal file
View File

@ -0,0 +1,24 @@
/*
* OpenCommand.hpp
*
* Created on: 4 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Command.hpp"
#include "Application.hpp"
#include <string>
class OpenCommand: public Command
{
private:
Application *_application;
std::string _response;
protected:
virtual std::string AskUser();
public:
OpenCommand(Application*);
virtual void Execute();
};

17
lesson_6/PasteCommand.cpp Normal file
View File

@ -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();
}

20
lesson_6/PasteCommand.hpp Normal file
View File

@ -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();
};

View File

@ -0,0 +1,15 @@
/*
* SimpleCommand.cpp
*
* Created on: 4 нояб. 2021 г.
* Author: alexander
*/
#include "SimpleCommand.hpp"
template<class Receiver>
void SimpleCommand<Receiver>::Execute()
{
(_receiver->*_action)();
}

View File

@ -0,0 +1,24 @@
/*
* SimpleCommand.hpp
*
* Created on: 4 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Command.hpp"
template<class Receiver>
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;
};

22
lesson_6/main.cpp Normal file
View File

@ -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;
}