7using System.Collections.Generic;
67 source.Text = source.
Text.Replace(searchPattern, replaceText);
76 StringBuilder output =
new StringBuilder();
77 string text = source.
Text;
78 for (
int index = 0; index < text.Length; ++index)
80 output.Append(text[text.Length - 1 - index]);
82 source.Text = output.ToString();
98 Console.WriteLine(
" undoing operation {0,-31}: \"{1}\"", lastMemento.
Name, text);
112 string operationName = String.Format(
"Replace '{0}' with '{1}'", searchPattern, replaceText);
115 Console.WriteLine(
" operation {0,-31}: \"{1}\"", operationName, text);
127 string operationName =
"Reverse";
130 Console.WriteLine(
" operation {0,-31}: \"{1}\"", operationName, text);
140 Console.WriteLine(
"Memento Exercise");
148 Console.WriteLine(
" Starting text: \"{0}\"", text);
156 Console.WriteLine(
" Now perform undo until back to original");
164 Console.WriteLine(
" Final text : \"{0}\"", text);
166 Console.WriteLine(
" Done.");
Example of using the Memento Pattern in C#.
void Memento_Operation_Reverse(Memento_TextObject source)
An operation to reverse the characters in the given Memento_TextObject.
void Memento_Undo(Memento_TextObject text)
Perform an undo on the given Command_TextObject, using the mementos in the "global" undo list....
void Run()
Executes the example for the Memento Pattern in C#.
void Memento_ApplyReplaceOperation(Memento_TextObject text, string searchPattern, string replaceText)
Helper function to replace a pattern with another string in the given Memento_TextObject after adding...
void Memento_ApplyReverseOperation(Memento_TextObject text)
Helper function to reverse the order of the characters in the given Memento_TextObject after adding a...
void Memento_Operation_Replace(Memento_TextObject source, string searchPattern, string replaceText)
An operation to search and replace text in a Memento_TextObject.
void Memento_SaveForUndo(Memento_TextObject text, string operation)
Take a snapshot of the given text object associated with the name of given operation.
Stack< IMemento > _mementoUndoList
The list of memento objects that form a series of snapshots in time of a Memento_TextObject.
Container for a string. Need to use a class that allows the text to be changed while the container (t...
void RestoreMemento(IMemento memento)
Sets the text in this class instance to the snapshot stored in the given IMemento object (which is as...
string Text
Gets or sets the text in this TextObject.
IMemento GetMemento(string operationName)
Returns an IMemento object containing a snapshot of the text stored in this class instance.
Represents a single memento, a single snapshot of the state of the Memento_TextObject class as repres...
string Name
The name of the memento (snapshot). Useful for displaying a list of mementos in an undo list....
The namespace containing all Design Pattern Examples implemented in C#.