-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.cpp
66 lines (48 loc) · 1.68 KB
/
Command.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//============================================================================
// Name : Command.h
// Created on : 12.04.2020
// Author : Tokmakov Andrey
// Version : 1.0
// Copyright : Your copyright notice
// Description : Command pattern tests
//============================================================================
#include "Command.h"
namespace Command_Pattern_Tests {
void Light::on() {
std::cout << "The light is ON" << std::endl;
}
void Light::off() {
std::cout << "The light is OFF" << std::endl;
}
LightOnCommand::LightOnCommand(std::shared_ptr<Light> light) : mLight(light) {
}
void LightOnCommand::execute() {
this->mLight->on();
}
LightOffCommand::LightOffCommand(std::shared_ptr<Light> light) : mLight(light) {
}
void LightOffCommand::execute() {
this->mLight->off();
}
void RemoteControl::setCommand(std::unique_ptr<ICommand> cmd) {
this->command = std::move(cmd);
}
void RemoteControl::buttonPressed() {
command->execute();
}
}
void Command_Pattern_Tests::Test()
{
// Receiver
std::shared_ptr<Light> light = std::make_shared<Light>();
std::shared_ptr<RemoteControl> control = std::make_shared<RemoteControl>();
// concrete Command objects
// std::unique_ptr<LightOnCommand> lightOn = std::make_unique<LightOnCommand>(new LightOnCommand(light));
std::unique_ptr<LightOnCommand> lightOn = std::make_unique<LightOnCommand>(light);
std::unique_ptr<LightOffCommand> lightOff = std::make_unique<LightOffCommand>(light);
// execute
control->setCommand(std::move(lightOn));
control->buttonPressed();
control->setCommand(std::move(lightOff));
control->buttonPressed();
}