mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from random import sample
|
|
from string import ascii_letters
|
|
|
|
from behavioral.memento.concrete_memento import ConcreteMemento
|
|
from behavioral.memento.memento import Memento
|
|
|
|
|
|
class Originator:
|
|
"""
|
|
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.
|
|
"""
|
|
|
|
"""
|
|
For the sake of simplicity, the originator's state is stored inside a single
|
|
variable.
|
|
"""
|
|
_state = None
|
|
|
|
def __init__(self, state: str) -> None:
|
|
self._state = state
|
|
print(f"Originator: My initial state is: {self._state}")
|
|
|
|
def do_something(self) -> None:
|
|
"""
|
|
The Originator's business logic may affect its internal state.
|
|
Therefore, the client should backup the state before launching methods
|
|
of the business logic via the save() method.
|
|
"""
|
|
print("Originator: I'm doing something important.")
|
|
self._state = self._generate_random_string(30)
|
|
print(f"Originator: and my state has changed to: {self._state}")
|
|
|
|
def _generate_random_string(self, length: int) -> str:
|
|
return "".join(sample(ascii_letters, length))
|
|
|
|
def save(self) -> Memento:
|
|
"""
|
|
Saves the current state inside a memento.
|
|
"""
|
|
return ConcreteMemento(self._state)
|
|
|
|
def restore(self, memento: Memento) -> None:
|
|
"""
|
|
Restores the Originator's state from a memento object.
|
|
"""
|
|
|
|
self._state = memento.get_state()
|
|
|
|
print(f"Originator: My state has changed to: {self._state}")
|