EN: The Originator holds some important state that may change over time. It also defines a method for saving the state inside a memento and another method for restoring the state from it. RU: Создатель содержит некоторое важное состояние, которое может со временем меняться. Он
| 20 | |
| 21 | |
| 22 | class Originator: |
| 23 | """ |
| 24 | EN: The Originator holds some important state that may change over time. It |
| 25 | also defines a method for saving the state inside a memento and another |
| 26 | method for restoring the state from it. |
| 27 | |
| 28 | RU: Создатель содержит некоторое важное состояние, которое может со временем |
| 29 | меняться. Он также объявляет метод сохранения состояния внутри снимка и |
| 30 | метод восстановления состояния из него. |
| 31 | """ |
| 32 | |
| 33 | _state = None |
| 34 | """ |
| 35 | EN: For the sake of simplicity, the originator's state is stored inside a |
| 36 | single variable. |
| 37 | |
| 38 | RU: Для удобства состояние создателя хранится внутри одной переменной. |
| 39 | """ |
| 40 | |
| 41 | def __init__(self, state: str) -> None: |
| 42 | self._state = state |
| 43 | print(f"Originator: My initial state is: {self._state}") |
| 44 | |
| 45 | def do_something(self) -> None: |
| 46 | """ |
| 47 | EN: The Originator's business logic may affect its internal state. |
| 48 | Therefore, the client should backup the state before launching methods |
| 49 | of the business logic via the save() method. |
| 50 | |
| 51 | RU: Бизнес-логика Создателя может повлиять на его внутреннее состояние. |
| 52 | Поэтому клиент должен выполнить резервное копирование состояния с |
| 53 | помощью метода save перед запуском методов бизнес-логики. |
| 54 | """ |
| 55 | |
| 56 | print("Originator: I'm doing something important.") |
| 57 | self._state = self._generate_random_string(30) |
| 58 | print(f"Originator: and my state has changed to: {self._state}") |
| 59 | |
| 60 | @staticmethod |
| 61 | def _generate_random_string(length: int = 10) -> str: |
| 62 | return "".join(sample(ascii_letters, length)) |
| 63 | |
| 64 | def save(self) -> Memento: |
| 65 | """ |
| 66 | EN: Saves the current state inside a memento. |
| 67 | |
| 68 | RU: Сохраняет текущее состояние внутри снимка. |
| 69 | """ |
| 70 | |
| 71 | return ConcreteMemento(self._state) |
| 72 | |
| 73 | def restore(self, memento: Memento) -> None: |
| 74 | """ |
| 75 | EN: Restores the Originator's state from a memento object. |
| 76 | |
| 77 | RU: Восстанавливает состояние Создателя из объекта снимка. |
| 78 | """ |
| 79 |