|
| 1 | +from typing import List |
| 2 | + |
| 3 | + |
| 4 | +# ----------- MEMENTO ----------- |
| 5 | +class Snapshot: |
| 6 | + """The Memento that stores the state of the Editor.""" |
| 7 | + |
| 8 | + def __init__(self, text: str, cursor_position: int): |
| 9 | + self._text = text |
| 10 | + self._cursor_position = cursor_position |
| 11 | + |
| 12 | + def get_state(self): |
| 13 | + """Internal state is only accessible to the Editor.""" |
| 14 | + return self._text, self._cursor_position |
| 15 | + |
| 16 | + |
| 17 | +# ----------- ORIGINATOR ----------- |
| 18 | +class Editor: |
| 19 | + """Originator that creates and restores from Snapshots.""" |
| 20 | + |
| 21 | + def __init__(self): |
| 22 | + self._text = "" |
| 23 | + self._cursor_position = 0 |
| 24 | + |
| 25 | + def type(self, new_text: str): |
| 26 | + self._text += new_text |
| 27 | + self._cursor_position = len(self._text) |
| 28 | + |
| 29 | + def get_content(self): |
| 30 | + return self._text |
| 31 | + |
| 32 | + def create_snapshot(self) -> Snapshot: |
| 33 | + return Snapshot(self._text, self._cursor_position) |
| 34 | + |
| 35 | + def restore(self, snapshot: Snapshot): |
| 36 | + self._text, self._cursor_position = snapshot.get_state() |
| 37 | + |
| 38 | + |
| 39 | +# ----------- CARETAKER ----------- |
| 40 | +class History: |
| 41 | + """Caretaker that stores snapshots for undo functionality.""" |
| 42 | + |
| 43 | + def __init__(self): |
| 44 | + self._snapshots: List[Snapshot] = [] |
| 45 | + |
| 46 | + def save(self, snapshot: Snapshot): |
| 47 | + self._snapshots.append(snapshot) |
| 48 | + |
| 49 | + def undo(self) -> Snapshot: |
| 50 | + if not self._snapshots: |
| 51 | + print("[Undo] No more states to undo.") |
| 52 | + return None |
| 53 | + snapshot = self._snapshots.pop() |
| 54 | + print("[Undo] Restoring previous state.") |
| 55 | + return snapshot |
| 56 | + |
| 57 | + |
| 58 | +# ----------- CLIENT CODE ----------- |
| 59 | +def main(): |
| 60 | + editor = Editor() |
| 61 | + history = History() |
| 62 | + |
| 63 | + editor.type("Hello") |
| 64 | + history.save(editor.create_snapshot()) |
| 65 | + |
| 66 | + editor.type(" World!") |
| 67 | + history.save(editor.create_snapshot()) |
| 68 | + |
| 69 | + print("[Editor] Current content:", editor.get_content()) # Hello World! |
| 70 | + |
| 71 | + editor.type(" Let's learn Memento.") |
| 72 | + print("[Editor] Current content:", editor.get_content()) # Hello World! Let's learn Memento. |
| 73 | + |
| 74 | + # Perform undo |
| 75 | + snapshot = history.undo() |
| 76 | + if snapshot: |
| 77 | + editor.restore(snapshot) |
| 78 | + print("[Editor] After undo:", editor.get_content()) # Hello World! |
| 79 | + |
| 80 | + # Another undo |
| 81 | + snapshot = history.undo() |
| 82 | + if snapshot: |
| 83 | + editor.restore(snapshot) |
| 84 | + print("[Editor] After second undo:", editor.get_content()) # Hello |
| 85 | + |
| 86 | + snapshot = history.undo() |
| 87 | + if snapshot: |
| 88 | + editor.restore(snapshot) |
| 89 | + print("[Editor] After Third undo:", editor.get_content()) |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments