Design Pattern Examples
Overview of object-oriented design patterns
Memento_Exercise Class Reference

Example of using the Memento Pattern in C#. More...

Collaboration diagram for Memento_Exercise:
Collaboration graph

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.
 

Detailed Description

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.

Member Function Documentation

◆ Memento_ApplyReplaceOperation()

void Memento_ApplyReplaceOperation ( Memento_TextObject  text,
string  searchPattern,
string  replaceText 
)
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.

Parameters
textThe Memento_TextObject to affect.
searchPatternWhat to look for in the Memento_TextObject.
replaceTextWhat 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().

◆ Memento_ApplyReverseOperation()

void Memento_ApplyReverseOperation ( Memento_TextObject  text)
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.

Parameters
textThe 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().

◆ Memento_Operation_Replace()

void Memento_Operation_Replace ( Memento_TextObject  source,
string  searchPattern,
string  replaceText 
)
inlineprivate

An operation to search and replace text in a Memento_TextObject.

Parameters
sourceThe Memento_TextObject to affect.
searchPatternWhat to look for in the Memento_TextObject.
replaceTextWhat to replace the searchPattern with.

Definition at line 65 of file Memento_Exercise.cs.

References Memento_TextObject.Text.

Referenced by Memento_Exercise.Memento_ApplyReplaceOperation().

◆ Memento_Operation_Reverse()

void Memento_Operation_Reverse ( Memento_TextObject  source)
inlineprivate

An operation to reverse the characters in the given Memento_TextObject.

Parameters
sourceThe Memento_TextObject to affect.

Definition at line 74 of file Memento_Exercise.cs.

References Memento_TextObject.Text.

Referenced by Memento_Exercise.Memento_ApplyReverseOperation().

◆ Memento_SaveForUndo()

void Memento_SaveForUndo ( Memento_TextObject  text,
string  operation 
)
inlineprivate

Take a snapshot of the given text object associated with the name of given operation.

Parameters
textThe Memento_TextObject to take a snapshot of.
operationA 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().

◆ Memento_Undo()

void Memento_Undo ( Memento_TextObject  text)
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.

Parameters
textThe 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().

◆ Run()

Member Data Documentation

◆ _mementoUndoList

Stack<IMemento> _mementoUndoList = new Stack<IMemento>()
private

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().


The documentation for this class was generated from the following file: