-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemento.py
72 lines (54 loc) · 1.72 KB
/
memento.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
brief:
Memento.
Without violating encapsulation,capture
and externalize an object's internal state so that the
object can be restored to this state later.
Originator <==> Caretaker
| |
Create and restore Memento Save and provide Memento
"""
# Memento. Responsible for storing the internal state of the Originator
# and providing the internal state of the Originator as needed.
class Memento:
def __init__(self, state):
self.state = state
def set_state(self, state):
self.state = state
def get_state(self):
return self.state
# Originator. It is responsible for recording the internal state of the
# current moment, defining which states belong to the backup scope,
# and creating and restoring the memo data.
class Originator:
def __init__(self, state):
self.state = state
def create_memento(self):
return Memento(self.state)
def restore_from_memento(self, memento):
self.state = memento.get_state();
def set_state(self, state):
self.state = state
def get_state(self):
return self.state
# Caretaker. Manage, save and provide the Memento.
class Caretaker:
def set_memento(self, memento):
self.memento = memento
def get_memento(self):
return self.memento
if __name__ == '__main__':
# Initialize.
org = Originator("A")
print("%s" %(org.get_state()))
# Save.
taker = Caretaker();
taker.set_memento(org.create_memento());
# Change.
org.set_state("B")
print("%s" %(org.get_state()))
# Restore.
org.restore_from_memento(taker.get_memento());
print("%s" %(org.get_state()))