-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_state.py
More file actions
83 lines (61 loc) · 1.87 KB
/
pattern_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'
from abc import ABCMeta, abstractmethod
class State():
__metaclass__ = ABCMeta
@abstractmethod
def write_code(self):
pass
class Morning(State):
def write_code(self, work):
if work.hour <= 12 and work.hour > 8:
print '上午工作,精神百倍'
else:
work.set_status(Noon())
work.write_code(work)
class Noon(State):
def write_code(self, work):
if work.hour <= 14 and work.hour>12 :
print '中午工作,困,想午休'
else:
work.set_status(Afternoon())
work.write_code(work)
class Afternoon(State):
def write_code(self, work):
if work.hour <= 18 and work.hour>14:
print '下午工作,状态不错'
else:
work.set_status(Eve())
work.write_code(work)
class Eve(State):
def write_code(self, work):
if work.hour <= 22 and work.hour>18:
print '加班了,状态不太好'
else:
work.set_status(Night())
work.write_code(work)
class Night(State):
def write_code(self, work):
if work.hour <= 8 or work.hour > 22:
print '不行了,要睡觉了'
else:
work.set_status(Morning())
work.write_code(work)
class Work():
def __init__(self, hour):
self.hour = hour
self.state = Morning()
def set_status(self, state):
self.state = state
def write_code(self, work):
self.state.write_code(work)
if __name__ == '__main__':
work = Work(10)
for hour in (3, 11, 12, 13, 14, 17, 19, 22, 23,12):
work.hour = hour
print '%d点,' % hour
work.write_code(work)