Design Pattern Examples
Overview of object-oriented design patterns
Command_Exercise.cpp
Go to the documentation of this file.
1
6
7#include <iostream>
8#include <sstream>
9#include <vector>
10
11#include "Command_Exercise.h"
12#include "Command_Classes.h"
13
14#include "helpers/formatstring.h"
15#include "helpers/replace.h"
16
17namespace // Anonymous
18{
19 using namespace DesignPatternExamples_cpp;
20
24 std::vector<Command> _commandUndoList;
25
32 {
33 _commandUndoList.push_back(command);
34 command.Execute();
35 }
36
43 static void Command_Operation_Replace(Command_TextObject::shared_ptr_t source, std::string searchPattern, std::string replaceText)
44 {
45 std::string newText = Helpers::Replace(source->Text(), searchPattern.c_str(), replaceText.c_str());
46 source->SetText(newText);
47 }
48
53 static void Command_Operation_Reverse(Command_TextObject::shared_ptr_t source)
54 {
55 std::ostringstream output;
56 std::string text = source->Text();
57 for (size_t index = 0; index < text.size(); ++index)
58 {
59 output << text[text.size() - 1 - index];
60 }
61 source->SetText(output.str());
62 }
63
69 void Command_Undo(Command_TextObject::shared_ptr_t text)
70 {
71 if (!_commandUndoList.empty())
72 {
73 // Reset the text to the starting point.
74 text->Reset();
75
76 // Get rid of the last command applied and remember it.
77 Command lastCommand = _commandUndoList.back();
78 _commandUndoList.pop_back();
79
80 // Now apply all remaining commands to the text in order
81 // (oldest to newest).
82 std::vector<Command>::iterator commandIter = std::begin(_commandUndoList);
83 while (commandIter != std::end(_commandUndoList))
84 {
85 (*commandIter).Execute();
86 commandIter++;
87 }
88
89 // Show off what we (un)did.
90 std::cout << Helpers::formatstring(" undoing command %-31s==> \"%s\"",
91 lastCommand.ToString().c_str(), text->ToString().c_str()) << std::endl;
92 }
93 }
94
103 void Command_ApplyReplaceCommand(Command_TextObject::shared_ptr_t text, std::string searchPattern, std::string replaceText)
104 {
105 Command command(text, "Replace", Command_Operation_Replace, searchPattern, replaceText);
107 std::cout << Helpers::formatstring(" command %-31s==> \"%s\"",
108 command.ToString().c_str(), text->ToString().c_str()) << std::endl;
109 }
110
111
119 void Command_ApplyReverseCommand(Command_TextObject::shared_ptr_t text)
120 {
121 Command command(text, "Reverse", Command_Operation_Reverse);
123 std::cout << Helpers::formatstring(" command %-31s==> \"%s\"",
124 command.ToString().c_str(), text->ToString().c_str()) << std::endl;
125 }
126
127} // end anonymous namespace
128
130{
131
144 // ! [Using Command in C++]
146 {
147 std::cout << std::endl;
148 std::cout << "Command Exercise" << std::endl;
149
150 // The base text object to work from.
151 Command_TextObject::shared_ptr_t text = std::make_shared<Command_TextObject>("This is a line of text on which to experiment.");
152
153 std::cout << Helpers::formatstring(" Starting text: \"%s\"", text->ToString().c_str()) << std::endl;
154
155 // Apply four operations to the text.
156 Command_ApplyReplaceCommand(text, "text", "painting");
157 Command_ApplyReplaceCommand(text, "on", "off");
159 Command_ApplyReplaceCommand(text, "i", "!");
160
161 std::cout << " Now perform undo until back to original" << std::endl;
162
163 // Now undo the four operations.
164 Command_Undo(text);
165 Command_Undo(text);
166 Command_Undo(text);
167 Command_Undo(text);
168
169 std::cout << Helpers::formatstring(" Final text : \"%s\"", text->ToString().c_str()) << std::endl;
170
171 std::cout << " Done." << std::endl;
172 }
173 // ! [Using Command in C++]
174
175
176} // end namespace
Implementation of the Command_TextObject and Command classes used in the Command Pattern.
void Command_Save_And_Execute(Command *command)
Save the given command on the undo list then execute the command on the text object with which the co...
static void Command_ApplyReplaceCommand(Command_TextObject *text, const char *searchPattern, const char *replaceText)
Helper function to create a Command object that replaces text in the given Command_TextObject,...
static StackEntry * _commandUndoList
The stack used to remember the commands for undo.
static void Command_Operation_Replace(Command_TextObject *source, const char *searchPattern, const char *replaceText)
An operation to search and replace text in a Command_TextObject.
static void Command_ApplyReverseCommand(Command_TextObject *text)
Helper function to create a Command object that reverses the order of the characters in the given Com...
static void Command_Undo(Command_TextObject *text)
Perform an undo on the given Command_TextObject, using the commands in the "global" undo list....
static void Command_Operation_Reverse(Command_TextObject *source)
An operation to reverse the characters in the given Command_TextObject.
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.
std::string ToString()
Convert this command to a string representation.
Declaration of the Command_Exercise() function as used in the Command Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Command_Exercise()
Example of using the Command design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
std::string Replace(const std::string &s, const char *str1, const char *str2, bool bCaseInsensitive)
Replace all occurrences of narrow string str1 with narrow string str2 in s. If str2 is empty then all...
Definition: replace.cpp:47