-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstate.py
More file actions
43 lines (34 loc) · 849 Bytes
/
state.py
File metadata and controls
43 lines (34 loc) · 849 Bytes
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
#-*- coding: utf-8 -*-
class State:
'''
@summary: 状态父类,用于抽象状态方法,
+每增加一种状态,只需要增加一个子类,并修改相关状态的跳转
@ivar ivar: type
@author: 0xnz
@since: 2013-9-25
@change:
'''
def __init__(self):
pass
def operate(self, w):
pass
class MorningState(State):
def operate(self, w):
if w.getHour() < 12:
print 'time now: %d, working' % w.getHour()
else:
w.setState(NoonState())
w.operate()
class NoonState(State):
def operate(self, w):
if w.getHour() < 14:
print 'time now: %d, eating launch' % w.getHour()
else:
w.setState(EveningState())
w.operate()
class EveningState(State):
def operate(self, w):
if w.getHour() < 18:
print 'time now: %d, eating dinner' % w.getHour()
else:
print 'time now: %d, watching TV' % w.getHour()