-
Notifications
You must be signed in to change notification settings - Fork 56
/
memento.py
44 lines (31 loc) · 987 Bytes
/
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
class StateMemento(object):
def __init__(self, h, m):
self.hp = h
self.mp = m
class GameRole(object):
def __init__(self):
self.hp = 100
self.mp = 100
def fight(self):
self.hp = 0
self.mp = 0
def create_memento(self):
return StateMemento(self.hp, self.mp)
def recovery_state(self, m):
self.hp = m.hp
self.mp = m.mp
def state_display(self):
print str(self.hp) + " " + str(self.mp)
class StateCaretaker(object):
def __init__(self, m):
self.__memento = m # .__memento ==> ._StateCaretaker__memento (Name Mangling)
def get_memento(self):
return self.__memento
if __name__ == "__main__":
game_role = GameRole()
state_caretaker = StateCaretaker(game_role.create_memento())
game_role.state_display()
game_role.fight()
game_role.state_display()
game_role.recovery_state(state_caretaker.get_memento())
game_role.state_display()