Design Pattern Examples
Overview of object-oriented design patterns
Command_Classes.h
Go to the documentation of this file.
1
6
7#pragma once
8#ifndef __COMMAND_CLASSES_H__
9#define __COMMAND_CLASSES_H__
10
11#include <memory>
12#include <string>
13
14#include "helpers/formatstring.h"
15
17{
18
27 {
28 public:
29 using shared_ptr_t = std::shared_ptr<Command_TextObject>;
30
31 private:
32 // Starting string text so we can reset the text to a known point.
33 std::string _startingText;
34
35 // The text that can change.
36 std::string _text;
37
38 public:
43 Command_TextObject(std::string text)
44 {
45 _text = text;
46 _startingText = text;
47 }
48
52 std::string Text() { return _text; }
56 void SetText(std::string value) { _text = value; }
57
61 void Reset()
62 {
64 }
65
70 std::string ToString()
71 {
72 return _text;
73 }
74 };
75
76
84 using two_parameter_operation = void (*)(Command_TextObject::shared_ptr_t source, std::string argument1, std::string argument2);
85
91 using no_parameter_operation = void (*)(Command_TextObject::shared_ptr_t source);
92
93
118 {
119 private:
120 // The receiver of the command.
121 Command_TextObject::shared_ptr_t _receiver;
122
123 // Easy-to-read command name.
124 std::string _commandName;
125
126 // Two parameter operation to apply to the receiver.
128
129 // No parameter operation to apply to the receiver.
131
132 // The first argument to the operation.
133 std::string _argument1;
134
135 // The second argument to the operation.
136 std::string _argument2;
137
138 public:
148 Command(Command_TextObject::shared_ptr_t source, const std::string& commandName, two_parameter_operation operation, const std::string& argument1, const std::string& argument2)
149 : _no_parameter_operation(nullptr)
150 {
151 _receiver = source;
152 _commandName = commandName;
153 _two_parameter_operation = operation;
154 _argument1 = argument1;
155 _argument2 = argument2;
156 }
157
165 Command(Command_TextObject::shared_ptr_t source, const std::string& commandName, no_parameter_operation operation)
166 : _two_parameter_operation(nullptr)
167 {
168 _receiver = source;
169 _commandName = commandName;
170 _no_parameter_operation = operation;
171 }
172
176 void Execute()
177 {
178 if (_two_parameter_operation != nullptr)
179 {
181 }
182 else // if (_no_parameter_operation != nullptr)
183 {
185 }
186 }
187
192 std::string ToString()
193 {
194 std::string output = "<NO COMMAND>";
195 if (_two_parameter_operation != nullptr)
196 {
197 output = Helpers::formatstring("%s \"%s\" with \"%s\"",
198 _commandName.c_str(), _argument1.c_str(), _argument2.c_str());
199 }
200 else if (_no_parameter_operation != nullptr)
201 {
202 output = Helpers::formatstring("%s", _commandName.c_str());
203 }
204 return output;
205 }
206 };
207
208} // end namespace
209
210#endif // __COMMAND_CLASSES_H__
211
Container for a string. Need to use a class that allows the text to be changed while the container (t...
void SetText(std::string value)
Sets the text in this TextObject.
void Reset()
Resets the TextObject to the starting string.
std::string Text()
Gets the text in this TextObject.
std::shared_ptr< Command_TextObject > shared_ptr_t
std::string ToString()
Converts the TextObject to a string.
Command_TextObject(std::string text)
Constructs a text object with an initial string.
Represents an operation that can be applied to a TextObject. This class can handle two kinds of opera...
void Execute()
Execute the command on the TextObject.
Command(Command_TextObject::shared_ptr_t source, const std::string &commandName, no_parameter_operation operation)
Constructor for a command that applies an operation to a TextObject but does not take any additional ...
Command_TextObject::shared_ptr_t _receiver
Command(Command_TextObject::shared_ptr_t source, const std::string &commandName, two_parameter_operation operation, const std::string &argument1, const std::string &argument2)
Constructor for a command that applies an operation to a TextObject, where the operation takes two pa...
std::string ToString()
Convert this command to a string representation.
no_parameter_operation _no_parameter_operation
two_parameter_operation _two_parameter_operation
The namespace containing all Design Pattern Examples implemented in C++.
void(*)(Command_TextObject::shared_ptr_t source) no_parameter_operation
Alias for a function type representing an operation applied to a TextObject that uses no additional a...
void(*)(Command_TextObject::shared_ptr_t source, std::string argument1, std::string argument2) two_parameter_operation
Alias for a function type representing an operation applied to a TextObject using two parameters.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....