Design Pattern Examples
Overview of object-oriented design patterns
memento_exercise.py
Go to the documentation of this file.
6
7from io import StringIO
8
9from .memento import IMemento, Memento_TextObject
10
11
13_mementoUndoList = [] # type: list[IMemento]
14
15
23def Memento_SaveForUndo(text : Memento_TextObject, operation : str) -> None:
24 memento = text.GetMemento(operation)
25 _mementoUndoList.append(memento)
26
27
28
36def Memento_Operation_Replace(source : Memento_TextObject, searchPattern : str, replaceText : str) -> None:
37 source.SetText = source.Text.replace(searchPattern, replaceText)
38
39
40
44def Memento_Operation_Reverse(source : Memento_TextObject) -> None:
45 output = StringIO()
46 text = source.Text
47 textLength = len(text)
48 for index in range(0, textLength):
49 output.write(text[textLength - 1 - index])
50 source.SetText = output.getvalue()
51
52
53
58def Memento_Undo(text : Memento_TextObject) -> None:
59 if _mementoUndoList:
60 lastMemento = _mementoUndoList.pop()
61 text.RestoreMemento(lastMemento)
62
63 # Show off what we (un)did.
64 print(" undoing operation {0:<31}: \"{1}\"".format(lastMemento.Name, text.ToString()))
65
66
67
77def Memento_ApplyReplaceOperation(text : Memento_TextObject, searchPattern : str, replaceText : str) -> None:
78 operationName = "Replace '{0}' with '{1}'".format(searchPattern, replaceText)
79 Memento_SaveForUndo(text, operationName)
80 Memento_Operation_Replace(text, searchPattern, replaceText)
81 print(" operation {0:<31}: \"{1}\"".format(operationName, text.ToString()))
82
83
84
90def Memento_ApplyReverseOperation(text : Memento_TextObject) -> None:
91 operationName = "Reverse"
92 Memento_SaveForUndo(text, operationName)
94 print(" operation {0:<31}: \"{1}\"".format(operationName, text.ToString()))
95
96
97
119
120# ! [Using Memento in Python]
122 print()
123 print("Memento Exercise")
124
125 # Start with a fresh undo list.
126 _mementoUndoList.clear()
127
128 # The base text object to work from.
129 text = Memento_TextObject("This is a line of text on which to experiment.")
130
131 print(" Starting text: \"{0}\"".format(text.ToString()))
132
133 # Apply four operations to the text.
134 Memento_ApplyReplaceOperation(text, "text", "painting")
135 Memento_ApplyReplaceOperation(text, "on", "off")
137 Memento_ApplyReplaceOperation(text, "i", "!")
138
139 print(" Now perform undo until back to original")
140
141 # Now undo the four operations.
142 Memento_Undo(text)
143 Memento_Undo(text)
144 Memento_Undo(text)
145 Memento_Undo(text)
146
147 print(" Final text : \"{0}\"".format(text.ToString()))
148
149 print(" Done.")
150# ! [Using Memento in Python]
None Memento_Operation_Reverse(Memento_TextObject source)
An operation to reverse the characters in the given Memento_TextObject.
None Memento_Operation_Replace(Memento_TextObject source, str searchPattern, str replaceText)
An operation to search and replace text in a Memento_TextObject.
None Memento_Undo(Memento_TextObject text)
Perform an undo on the given Command_TextObject, using the mementos in the "global" undo list.
def Memento_Exercise()
Example of using the Memento Pattern.
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_ApplyReverseOperation(Memento_TextObject text)
Helper function to reverse the order of the characters in the given Memento_TextObject after adding a...
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...