8#ifndef __NULL_OBJECT_H__
9#define __NULL_OBJECT_H__
16#include "helpers/formatstring.h"
131 std::cout <<
"move left";
161 std::cout <<
"move right";
191 std::cout <<
"move up";
221 std::cout <<
"move down";
289 std::vector<MoveCommand::shared_ptr_t>
_ParseMoves(std::string moveList)
291 std::vector<MoveCommand::shared_ptr_t> commands;
292 for (
size_t index = 0; index < moveList.size(); index++)
294 char commandChar =
static_cast<char>(std::toupper(moveList[index]));
295 std::string command(1, commandChar);
301 moveCommand = std::make_shared<MoveCommandUp>(command);
305 moveCommand = std::make_shared<MoveCommandDown>(command);
309 moveCommand = std::make_shared<MoveCommandLeft>(command);
313 moveCommand = std::make_shared<MoveCommandRight>(command);
318 moveCommand = std::make_shared<MoveCommandNone>(command);
321 commands.push_back(moveCommand);
347 std::cout << std::endl;
356 void _ShowMoves(
const std::vector<MoveCommand::shared_ptr_t>& commands)
378 std::vector<MoveCommand::shared_ptr_t> commands =
_ParseMoves(moveList);
393 std::vector<MoveCommand::shared_ptr_t> commands =
_ParseMoves(moveList);
Represents the Move Down command.
MoveCommandDown(std::string command)
Constructor.
void Execute() override
Executes the move down command.
Base class that represents a move command. A move command has a name and the command character that r...
virtual ~MoveCommand()
Virtual destructor required for interfaces/base classes.
std::string _command
The command for controlling movement.
std::string Command()
The command character from the original list of commands. Used when displaying the commands as oppose...
std::shared_ptr< MoveCommand > shared_ptr_t
Alias to make it easier to work with a shared pointer.
std::string _name
Name of the command.
MoveCommand(std::string command, std::string commandName)
Constructor.
virtual void Show()
Display the move command and its name followed by a newline.
virtual void Execute()=0
Execute the command. Derived classes must implement this.
std::string Name()
Name of the command (assigned in the class constructor).
Represents the Move Left command.
MoveCommandLeft(std::string command)
Constructor.
void Execute() override
Executes the move left command.
Represents the Do Nothing command. This is the Null Object for this exercise.
MoveCommandNone(std::string command)
Constructor.
void Execute() override
Does nothing when executed (this is the Null Object, after all).
Represents the Move Right command.
MoveCommandRight(std::string command)
Constructor.
void Execute() override
Executes the move right command.
Represents the Move Up command.
MoveCommandUp(std::string command)
Constructor.
void Execute() override
Executes the move up command.
Represents the processor that translates the move list into a list of MoveCommand objects then either...
void _ExecuteMoves(const std::vector< MoveCommand::shared_ptr_t > &commands)
Helper method to execute all the given commands.
void ExecuteMoveList(std::string moveList)
Parse and execute the given list of move commands, where each command is represents by a single chara...
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.
void ShowMoveList(std::string moveList)
Parse and display the given list of move commands, where each command is represents by a single chara...
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.
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....