-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemento_pattern.py
More file actions
39 lines (27 loc) · 817 Bytes
/
memento_pattern.py
File metadata and controls
39 lines (27 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Editor:
def __init__(self):
self.text = ""
def type(self, word) -> None:
self.text += word
def save(self) -> str:
return self.text
def restore(self, snapshot) -> None:
self.text = snapshot
class SnapshotHistory:
def __init__(self):
self.history = []
def push(self, snapshot) -> None:
self.history.append(snapshot)
def pop(self) -> None:
return self.history.pop() if self.history else None
if __name__ == '__main__':
editor = Editor()
history = SnapshotHistory()
editor.type("Hello ")
editor.type("World!")
history.push(editor.save())
print(f"1: {editor.text}")
editor.type(" More text.")
print(f"2: {editor.text}")
editor.restore(history.pop())
print(f"3: {editor.text}")