Design Pattern Examples
Overview of object-oriented design patterns
DesignPatternExamples_python.memento.memento_exercise Namespace Reference

Functions

None Memento_SaveForUndo (Memento_TextObject text, str operation)
 Take a snapshot of the given text object associated with the name of given operation.
 
None Memento_Operation_Replace (Memento_TextObject source, str searchPattern, str replaceText)
 An operation to search and replace text in a Memento_TextObject.
 
None Memento_Operation_Reverse (Memento_TextObject source)
 An operation to reverse the characters in the given Memento_TextObject.
 
None Memento_Undo (Memento_TextObject text)
 Perform an undo on the given Command_TextObject, using the mementos in the "global" undo list.
 
None Memento_ApplyReplaceOperation (Memento_TextObject text, str searchPattern, str 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.
 
None 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.
 
def Memento_Exercise ()
 Example of using the Memento Pattern.
 

Variables

list _mementoUndoList = []
 The list of memento objects that form a series of snapshots in time of a Memento_TextObject.
 

Function Documentation

◆ Memento_ApplyReplaceOperation()

None Memento_ApplyReplaceOperation ( Memento_TextObject  text,
str  searchPattern,
str  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.

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

Definition at line 77 of file memento_exercise.py.

References DesignPatternExamples_python.memento.memento_exercise.Memento_Operation_Replace(), and DesignPatternExamples_python.memento.memento_exercise.Memento_SaveForUndo().

Referenced by DesignPatternExamples_python.memento.memento_exercise.Memento_Exercise().

◆ Memento_ApplyReverseOperation()

None 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.

Parameters
textThe Memento_TextObject to affect.

Definition at line 90 of file memento_exercise.py.

References DesignPatternExamples_python.memento.memento_exercise.Memento_Operation_Reverse(), and DesignPatternExamples_python.memento.memento_exercise.Memento_SaveForUndo().

Referenced by DesignPatternExamples_python.memento.memento_exercise.Memento_Exercise().

◆ Memento_Exercise()

def Memento_Exercise ( void  )

Example of using the Memento Pattern.

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 121 of file memento_exercise.py.

References DesignPatternExamples_python.memento.memento_exercise.Memento_ApplyReplaceOperation(), DesignPatternExamples_python.memento.memento_exercise.Memento_ApplyReverseOperation(), and DesignPatternExamples_python.memento.memento_exercise.Memento_Undo().

◆ Memento_Operation_Replace()

None Memento_Operation_Replace ( Memento_TextObject  source,
str  searchPattern,
str  replaceText 
)

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 36 of file memento_exercise.py.

Referenced by DesignPatternExamples_python.memento.memento_exercise.Memento_ApplyReplaceOperation().

◆ Memento_Operation_Reverse()

None Memento_Operation_Reverse ( Memento_TextObject  source)

An operation to reverse the characters in the given Memento_TextObject.

Parameters
sourceThe Memento_TextObject to affect.

Definition at line 44 of file memento_exercise.py.

Referenced by DesignPatternExamples_python.memento.memento_exercise.Memento_ApplyReverseOperation().

◆ Memento_SaveForUndo()

None Memento_SaveForUndo ( Memento_TextObject  text,
str  operation 
)

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 23 of file memento_exercise.py.

Referenced by DesignPatternExamples_python.memento.memento_exercise.Memento_ApplyReplaceOperation(), and DesignPatternExamples_python.memento.memento_exercise.Memento_ApplyReverseOperation().

◆ Memento_Undo()

None 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.

Parameters
textThe Command_TextObject to affect.

Definition at line 58 of file memento_exercise.py.

Referenced by DesignPatternExamples_python.memento.memento_exercise.Memento_Exercise().

Variable Documentation

◆ _mementoUndoList

list _mementoUndoList = []
protected

The list of memento objects that form a series of snapshots in time of a Memento_TextObject.

Definition at line 13 of file memento_exercise.py.