Example of using the Memento Pattern in C#. More...
Public Member Functions | |
void | Run () |
Executes the example for the Memento Pattern in C#. | |
Private Member Functions | |
void | Memento_SaveForUndo (Memento_TextObject text, string operation) |
Take a snapshot of the given text object associated with the name of given operation. | |
void | Memento_Operation_Replace (Memento_TextObject source, string searchPattern, string replaceText) |
An operation to search and replace text in a Memento_TextObject. | |
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. If the undo list is empty, nothing happens. | |
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 a snapshot of the text object to the undo list. Finally, it shows off what was done. | |
void | Memento_ApplyReverseOperation (Memento_TextObject text) |
Helper function to reverse the order of the characters in the given Memento_TextObject after adding a snapshot of the text object to an undo list. Finally, it shows what was done. | |
Private Attributes | |
Stack< IMemento > | _mementoUndoList = new Stack<IMemento>() |
The list of memento objects that form a series of snapshots in time of a Memento_TextObject. | |
Example of using the Memento Pattern in C#.
In this exercise, the Memento pattern is used to take snapshots of a text object so as to form an undo list of changes to the text object. Undoing an operation means restoring a snapshot of the text object.
The undo list is implemented as a stack of memento objects that each represent a snapshot of the text object taken before each operation is applied. After all operations are applied, the mementos are used to restore the text object in reverse order, effectively undoing each operation in turn.
Compare this to the Command_Exercise() and note that the steps taken there are identical to here (except for method names, of course). The difference lies in how operations are executed and undone. Mementos make the undo process much cleaner and faster since operations do not need to be applied repeatedly to get the text object into a specific state. Specifically, compare Command_Undo() with Memento_Undo(). Also note the differences in the "Memento_ApplyXXOperation()" methods, which more cleanly separate the save from the operation.
Definition at line 36 of file Memento_Exercise.cs.
|
inlineprivate |
Helper function to replace a pattern with another string in the given Memento_TextObject after adding a snapshot of the text object to the undo list. Finally, it shows off what was done.
text | The Memento_TextObject to affect. |
searchPattern | What to look for in the Memento_TextObject. |
replaceText | What to replace the searchPattern with. |
Definition at line 110 of file Memento_Exercise.cs.
References Memento_Exercise.Memento_Operation_Replace(), and Memento_Exercise.Memento_SaveForUndo().
Referenced by Memento_Exercise.Run().
|
inlineprivate |
Helper function to reverse the order of the characters in the given Memento_TextObject after adding a snapshot of the text object to an undo list. Finally, it shows what was done.
text | The Memento_TextObject to affect. |
Definition at line 125 of file Memento_Exercise.cs.
References Memento_Exercise.Memento_Operation_Reverse(), and Memento_Exercise.Memento_SaveForUndo().
Referenced by Memento_Exercise.Run().
|
inlineprivate |
An operation to search and replace text in a Memento_TextObject.
source | The Memento_TextObject to affect. |
searchPattern | What to look for in the Memento_TextObject. |
replaceText | What to replace the searchPattern with. |
Definition at line 65 of file Memento_Exercise.cs.
References Memento_TextObject.Text.
Referenced by Memento_Exercise.Memento_ApplyReplaceOperation().
|
inlineprivate |
An operation to reverse the characters in the given Memento_TextObject.
source | The Memento_TextObject to affect. |
Definition at line 74 of file Memento_Exercise.cs.
References Memento_TextObject.Text.
Referenced by Memento_Exercise.Memento_ApplyReverseOperation().
|
inlineprivate |
Take a snapshot of the given text object associated with the name of given operation.
text | The Memento_TextObject to take a snapshot of. |
operation | A string describing the operation that will be applied after the snapshot is taken. |
Definition at line 52 of file Memento_Exercise.cs.
References Memento_Exercise._mementoUndoList, and Memento_TextObject.GetMemento().
Referenced by Memento_Exercise.Memento_ApplyReplaceOperation(), and Memento_Exercise.Memento_ApplyReverseOperation().
|
inlineprivate |
Perform an undo on the given Command_TextObject, using the mementos in the "global" undo list. If the undo list is empty, nothing happens.
text | The Command_TextObject to affect. |
Definition at line 90 of file Memento_Exercise.cs.
References Memento_Exercise._mementoUndoList, IMemento.Name, and Memento_TextObject.RestoreMemento().
Referenced by Memento_Exercise.Run().
|
inline |
Executes the example for the Memento Pattern in C#.
Definition at line 137 of file Memento_Exercise.cs.
References Memento_Exercise._mementoUndoList, Memento_Exercise.Memento_ApplyReplaceOperation(), Memento_Exercise.Memento_ApplyReverseOperation(), and Memento_Exercise.Memento_Undo().
Referenced by Program.Run().
The list of memento objects that form a series of snapshots in time of a Memento_TextObject.
Definition at line 43 of file Memento_Exercise.cs.
Referenced by Memento_Exercise.Memento_SaveForUndo(), Memento_Exercise.Memento_Undo(), and Memento_Exercise.Run().