Design Pattern Examples
Overview of object-oriented design patterns
memento.py
Go to the documentation of this file.
10
11from abc import ABC, abstractmethod
12
13
14
18class IMemento(ABC):
19
23 @property
24 @abstractmethod
25 def Name(self) -> str:
26 pass
27
28
29
31
32
33
39 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
40 # Inner class Memento
41
42
46
52 def __init__(self, name : str, text : str) -> None:
53 self._name = name
54 self._text = text
55
56
63
64
65
70 @property
71 def Text(self) -> str:
72 return self._text
73
74
77 @property
78 def Name(self) -> str:
79 return self._name
80
81 # End of Inner class Memento
82 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
83 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
84
85
86
88
89
90 @property
91 def Text(self) -> str:
92 return self._text
93
94
95
96 @Text.setter
97 def Text(self, value : str) -> None:
98 self._text = value;
99
100
101
102
106 def __init__(self, text : str) -> None:
107 self._text = text;
108
109
113
114
120 def GetMemento(self, operationName : str) -> IMemento:
121 return Memento_TextObject.Memento(operationName, self._text)
122
123
124
129 def RestoreMemento(self, memento : IMemento) -> None:
130 if memento:
131 self._text = memento.Text
132
133
134
139 def ToString(self) -> str:
140 return self._text;
141
Represents a single memento, a single snapshot of the state of the Memento_TextObject class as repres...
Definition: memento.py:18
str Name(self)
Property getter for the name of the memento (snapshot): value = o.Name.
Definition: memento.py:25
Represents a single memento (snapshot) of the text state before an operation is applied.
Definition: memento.py:45
None __init__(self, str name, str text)
Constructor.
Definition: memento.py:52
_name
The name of this memento (really just the name of the operation that triggered the need for this meme...
Definition: memento.py:53
str Name(self)
Property getter for the name of this memento: value = o.Name.
Definition: memento.py:78
_text
The snapshot of the text data as stored in the Memento_TextObject class instance.
Definition: memento.py:54
str ToString(self)
Converts the Memento_TextObject to a string (makes it easier to use the class in string formatting).
Definition: memento.py:139
None __init__(self, str text)
Constructs a text object with an initial string.
Definition: memento.py:106
_text
The text that can change in this Memento_TextObject class.
Definition: memento.py:98
None RestoreMemento(self, IMemento memento)
Sets the text in this class instance to the snapshot stored in the given IMemento object (which is as...
Definition: memento.py:129
IMemento GetMemento(self, str operationName)
Returns an IMemento object containing a snapshot of the text stored in this class instance.
Definition: memento.py:120
Container for a string.