- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_state.py
More file actions
Latest commit
83 lines (61 loc) · 1.87 KB
/
Copy pathpattern_state.py
File metadata and controls
83 lines (61 loc) · 1.87 KB
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
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/2/5 15:05
# @Author : ZhangSheng@xiaoyezi.com
# @File : pattern_state.py
__author__='kevinlu1010@qq.com'
fromabcimportABCMeta, abstractmethod
classState():
__metaclass__=ABCMeta
@abstractmethod
defwrite_code(self):
pass
classMorning(State):
defwrite_code(self, work):
ifwork.hour<=12andwork.hour>8:
print'上午工作,精神百倍'
else:
work.set_status(Noon())
work.write_code(work)
classNoon(State):
defwrite_code(self, work):
ifwork.hour<=14andwork.hour>12 :
print'中午工作,困,想午休'
else:
work.set_status(Afternoon())
work.write_code(work)
classAfternoon(State):
defwrite_code(self, work):
ifwork.hour<=18andwork.hour>14:
print'下午工作,状态不错'
else:
work.set_status(Eve())
work.write_code(work)
classEve(State):
defwrite_code(self, work):
ifwork.hour<=22andwork.hour>18:
print'加班了,状态不太好'
else:
work.set_status(Night())
work.write_code(work)
classNight(State):
defwrite_code(self, work):
ifwork.hour<=8orwork.hour>22:
print'不行了,要睡觉了'
else:
work.set_status(Morning())
work.write_code(work)
classWork():
def__init__(self, hour):
self.hour=hour
self.state=Morning()
defset_status(self, state):
self.state=state
defwrite_code(self, work):
self.state.write_code(work)
if__name__=='__main__':
work=Work(10)
forhourin (3, 11, 12, 13, 14, 17, 19, 22, 23,12):
work.hour=hour
print'%d点,'%hour
work.write_code(work)