9.1. Memento¶
EN: Memento
PL: Pamiątka
Type: object
9.1.1. Pattern¶
Undo operation
Remembering state of objects

9.1.2. Problem¶

9.1.3. Solution¶

from dataclasses import dataclass, field
from typing import Final
@dataclass
class EditorState:
content: Final[str]
def get_content(self):
return self.content
@dataclass
class History:
states: list[EditorState] = field(default_factory=list)
def push(self, state: EditorState) -> None:
self.states.append(state)
def pop(self) -> EditorState:
return self.states.pop()
class Editor:
content: str
def set_content(self, content: str) -> None:
self.content = content
def get_content(self) -> str:
return self.content
def create_state(self):
return EditorState(self.content)
def restore_state(self, state: EditorState):
self.content = state.get_content()
if __name__ == '__main__':
editor = Editor()
history = History()
editor.set_content('a')
history.push(editor.create_state())
print(editor.get_content())
# a
editor.set_content('b')
history.push(editor.create_state())
print(editor.get_content())
# b
editor.set_content('c')
print(editor.get_content())
# c
editor.restore_state(history.pop())
print(editor.get_content())
# b
9.1.4. Assignments¶
"""
* Assignment: DesignPatterns Behavioral Memento
* Complexity: medium
* Lines of code: 10 lines
* Time: 13 min
English:
1. Implement Memento pattern
2. Run doctests - all must succeed
Polish:
1. Zaimplementuj wzorzec Memento
2. Uruchom doctesty - wszystkie muszą się powieść
Tests:
>>> account = Account()
>>> history = History()
>>> account.deposit(100.00)
>>> history.push(account.save())
>>> account.get_balance()
100.0
>>> account.deposit(50.00)
>>> history.push(account.save())
>>> account.get_balance()
150.0
>>> account.deposit(25.00)
>>> account.get_balance()
175.0
>>> account.undo(history.pop())
>>> account.get_balance()
150.0
"""
from dataclasses import dataclass, field
from typing import Final
@dataclass
class Transaction:
def get_amount(self):
raise NotImplementedError
@dataclass
class History:
def push(self, transaction: Transaction) -> None:
raise NotImplementedError
def pop(self) -> Transaction:
raise NotImplementedError
@dataclass
class Account:
def deposit(self, amount: float) -> None:
raise NotImplementedError
def get_balance(self) -> float:
raise NotImplementedError
def save(self):
raise NotImplementedError
def undo(self, transaction: Transaction):
raise NotImplementedError