Design Pattern Examples
Overview of object-oriented design patterns
Memento_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9
11{
36 internal class Memento_Exercise
37 {
38
43 Stack<IMemento> _mementoUndoList = new Stack<IMemento>();
44
52 void Memento_SaveForUndo(Memento_TextObject text, string operation)
53 {
54 IMemento memento = text.GetMemento(operation);
55 _mementoUndoList.Push(memento);
56 }
57
58
65 void Memento_Operation_Replace(Memento_TextObject source, string searchPattern, string replaceText)
66 {
67 source.Text = source.Text.Replace(searchPattern, replaceText);
68 }
69
75 {
76 StringBuilder output = new StringBuilder();
77 string text = source.Text;
78 for (int index = 0; index < text.Length; ++index)
79 {
80 output.Append(text[text.Length - 1 - index]);
81 }
82 source.Text = output.ToString();
83 }
84
91 {
92 if (_mementoUndoList.Count > 0)
93 {
94 IMemento lastMemento = _mementoUndoList.Pop();
95 text.RestoreMemento(lastMemento);
96
97 // Show off what we (un)did.
98 Console.WriteLine(" undoing operation {0,-31}: \"{1}\"", lastMemento.Name, text);
99 }
100 }
101
110 void Memento_ApplyReplaceOperation(Memento_TextObject text, string searchPattern, string replaceText)
111 {
112 string operationName = String.Format("Replace '{0}' with '{1}'", searchPattern, replaceText);
113 Memento_SaveForUndo(text, operationName);
114 Memento_Operation_Replace(text, searchPattern, replaceText);
115 Console.WriteLine(" operation {0,-31}: \"{1}\"", operationName, text);
116 }
117
118
126 {
127 string operationName = "Reverse";
128 Memento_SaveForUndo(text, operationName);
130 Console.WriteLine(" operation {0,-31}: \"{1}\"", operationName, text);
131 }
132
136 // ! [Using Memento in C#]
137 public void Run()
138 {
139 Console.WriteLine();
140 Console.WriteLine("Memento Exercise");
141
142 // Start with a fresh undo list.
143 _mementoUndoList.Clear();
144
145 // The base text object to work from.
146 Memento_TextObject text = new Memento_TextObject("This is a line of text on which to experiment.");
147
148 Console.WriteLine(" Starting text: \"{0}\"", text);
149
150 // Apply four operations to the text.
151 Memento_ApplyReplaceOperation(text, "text", "painting");
152 Memento_ApplyReplaceOperation(text, "on", "off");
154 Memento_ApplyReplaceOperation(text, "i", "!");
155
156 Console.WriteLine(" Now perform undo until back to original");
157
158 // Now undo the four operations.
159 Memento_Undo(text);
160 Memento_Undo(text);
161 Memento_Undo(text);
162 Memento_Undo(text);
163
164 Console.WriteLine(" Final text : \"{0}\"", text);
165
166 Console.WriteLine(" Done.");
167 }
168 // ! [Using Memento in C#]
169 }
170}
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...
Definition: Memento.cs:37
void RestoreMemento(IMemento memento)
Sets the text in this class instance to the snapshot stored in the given IMemento object (which is as...
Definition: Memento.cs:156
string Text
Gets or sets the text in this TextObject.
Definition: Memento.cs:130
IMemento GetMemento(string operationName)
Returns an IMemento object containing a snapshot of the text stored in this class instance.
Definition: Memento.cs:145
Represents a single memento, a single snapshot of the state of the Memento_TextObject class as repres...
Definition: Memento.cs:14
string Name
The name of the memento (snapshot). Useful for displaying a list of mementos in an undo list....
Definition: Memento.cs:21
The namespace containing all Design Pattern Examples implemented in C#.