Design Pattern Examples
Overview of object-oriented design patterns
Null_Object.h
Go to the documentation of this file.
1
6
7#pragma once
8#ifndef __NULL_OBJECT_H__
9#define __NULL_OBJECT_H__
10
11#include <iostream>
12#include <memory>
13#include <string>
14#include <vector>
15
16#include "helpers/formatstring.h"
17
19{
20
33 {
34 public:
38 using shared_ptr_t = std::shared_ptr<MoveCommand>;
39
40 private:
44 std::string _name;
45
49 std::string _command;
50
51 public:
55 virtual ~MoveCommand() { }
56
60 std::string Name()
61 {
62 return _name;
63 }
64
70 std::string Command()
71 {
72 return _command;
73 }
74
75
81 MoveCommand(std::string command, std::string commandName)
82 : _name(commandName)
83 , _command(command)
84 {
85 }
86
87
91 virtual void Show()
92 {
93 std::cout
94 << Helpers::formatstring(" '%s' -> %s", _command.c_str(), _name.c_str())
95 << std::endl;
96 }
97
98
102 virtual void Execute() = 0;
103 };
104
105
106 //########################################################################
107 //########################################################################
108
109
114 {
115 public:
121 MoveCommandLeft(std::string command) : MoveCommand(command, "Left")
122 {
123 }
124
125
129 void Execute() override
130 {
131 std::cout << "move left";
132 }
133 };
134
135
136 //########################################################################
137 //########################################################################
138
139
144 {
145 public:
151 MoveCommandRight(std::string command) : MoveCommand(command, "Right")
152 {
153 }
154
155
159 void Execute() override
160 {
161 std::cout << "move right";
162 }
163 };
164
165
166 //########################################################################
167 //########################################################################
168
169
174 {
175 public:
181 MoveCommandUp(std::string command) : MoveCommand(command, "Up")
182 {
183 }
184
185
189 void Execute() override
190 {
191 std::cout << "move up";
192 }
193 };
194
195
196 //########################################################################
197 //########################################################################
198
199
204 {
205 public:
211 MoveCommandDown(std::string command) : MoveCommand(command, "Down")
212 {
213 }
214
215
219 void Execute() override
220 {
221 std::cout << "move down";
222 }
223 };
224
225
226 //########################################################################
227 //########################################################################
228
229
235 {
236 public:
242 MoveCommandNone(std::string command) : MoveCommand(command, "None")
243 {
244 }
245
246
250 void Execute() override
251 {
252 }
253 };
254
255
256 //########################################################################
257 //########################################################################
258
259
274 {
275 private:
276 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
277 // Private methods.
278
289 std::vector<MoveCommand::shared_ptr_t> _ParseMoves(std::string moveList)
290 {
291 std::vector<MoveCommand::shared_ptr_t> commands;
292 for (size_t index = 0; index < moveList.size(); index++)
293 {
294 char commandChar = static_cast<char>(std::toupper(moveList[index]));
295 std::string command(1, commandChar);
296 MoveCommand::shared_ptr_t moveCommand;
297
298 switch (commandChar)
299 {
300 case 'U':
301 moveCommand = std::make_shared<MoveCommandUp>(command);
302 break;
303
304 case 'D':
305 moveCommand = std::make_shared<MoveCommandDown>(command);
306 break;
307
308 case 'L':
309 moveCommand = std::make_shared<MoveCommandLeft>(command);
310 break;
311
312 case 'R':
313 moveCommand = std::make_shared<MoveCommandRight>(command);
314 break;
315
316 default:
317 // Everything else is a "do nothing" command.
318 moveCommand = std::make_shared<MoveCommandNone>(command);
319 break;
320 }
321 commands.push_back(moveCommand);
322 }
323
324 return commands;
325 }
326
327
339 void _ExecuteMoves(const std::vector<MoveCommand::shared_ptr_t>& commands)
340 {
341 for(MoveCommand::shared_ptr_t command : commands)
342 {
343 std::cout << "<";
344 command->Execute();
345 std::cout << "> ";
346 }
347 std::cout << std::endl;
348 }
349
350
356 void _ShowMoves(const std::vector<MoveCommand::shared_ptr_t>& commands)
357 {
358 for (MoveCommand::shared_ptr_t command : commands)
359 {
360 command->Show();
361 }
362 }
363
364 public:
365 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
366 // Public methods.
367
376 void ExecuteMoveList(std::string moveList)
377 {
378 std::vector<MoveCommand::shared_ptr_t> commands = _ParseMoves(moveList);
379 _ExecuteMoves(commands);
380 }
381
382
391 void ShowMoveList(std::string moveList)
392 {
393 std::vector<MoveCommand::shared_ptr_t> commands = _ParseMoves(moveList);
394 _ShowMoves(commands);
395 }
396 };
397
398} // end namespace
399
400#endif // __NULL_OBJECT_H__
401
Represents the Move Down command.
Definition: Null_Object.h:204
MoveCommandDown(std::string command)
Constructor.
Definition: Null_Object.h:211
void Execute() override
Executes the move down command.
Definition: Null_Object.h:219
Base class that represents a move command. A move command has a name and the command character that r...
Definition: Null_Object.h:33
virtual ~MoveCommand()
Virtual destructor required for interfaces/base classes.
Definition: Null_Object.h:55
std::string _command
The command for controlling movement.
Definition: Null_Object.h:49
std::string Command()
The command character from the original list of commands. Used when displaying the commands as oppose...
Definition: Null_Object.h:70
std::shared_ptr< MoveCommand > shared_ptr_t
Alias to make it easier to work with a shared pointer.
Definition: Null_Object.h:38
std::string _name
Name of the command.
Definition: Null_Object.h:44
MoveCommand(std::string command, std::string commandName)
Constructor.
Definition: Null_Object.h:81
virtual void Show()
Display the move command and its name followed by a newline.
Definition: Null_Object.h:91
virtual void Execute()=0
Execute the command. Derived classes must implement this.
std::string Name()
Name of the command (assigned in the class constructor).
Definition: Null_Object.h:60
Represents the Move Left command.
Definition: Null_Object.h:114
MoveCommandLeft(std::string command)
Constructor.
Definition: Null_Object.h:121
void Execute() override
Executes the move left command.
Definition: Null_Object.h:129
Represents the Do Nothing command. This is the Null Object for this exercise.
Definition: Null_Object.h:235
MoveCommandNone(std::string command)
Constructor.
Definition: Null_Object.h:242
void Execute() override
Does nothing when executed (this is the Null Object, after all).
Definition: Null_Object.h:250
Represents the Move Right command.
Definition: Null_Object.h:144
MoveCommandRight(std::string command)
Constructor.
Definition: Null_Object.h:151
void Execute() override
Executes the move right command.
Definition: Null_Object.h:159
Represents the Move Up command.
Definition: Null_Object.h:174
MoveCommandUp(std::string command)
Constructor.
Definition: Null_Object.h:181
void Execute() override
Executes the move up command.
Definition: Null_Object.h:189
Represents the processor that translates the move list into a list of MoveCommand objects then either...
Definition: Null_Object.h:274
void _ExecuteMoves(const std::vector< MoveCommand::shared_ptr_t > &commands)
Helper method to execute all the given commands.
Definition: Null_Object.h:339
void ExecuteMoveList(std::string moveList)
Parse and execute the given list of move commands, where each command is represents by a single chara...
Definition: Null_Object.h:376
void _ShowMoves(const std::vector< MoveCommand::shared_ptr_t > &commands)
Display the command character and name of the command for each command in the given list of commands.
Definition: Null_Object.h:356
void ShowMoveList(std::string moveList)
Parse and display the given list of move commands, where each command is represents by a single chara...
Definition: Null_Object.h:391
std::vector< MoveCommand::shared_ptr_t > _ParseMoves(std::string moveList)
Helper method to convert a list of single letter commands into a list of MoveCommand objects.
Definition: Null_Object.h:289
The namespace containing all Design Pattern Examples implemented in C++.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....