-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.py
66 lines (47 loc) · 1.19 KB
/
status.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
#!/usr/bin/env python
"""
状态模式
"""
class State:
def __init__(self):
pass
def get_msg(self, w):
pass
class Work:
def __init__(self, status=None):
if status is None:
status = []
self.hour = 9
self.curr = StateA()
self.status = status
def set_state(self, s):
self.curr = s
def speak(self):
for stat in self.status:
stat.get_msg(self)
class StateA(State):
def get_msg(self, w):
if 0 <= w.hour < 7:
print(f"当前时间:{w.hour},在半夜")
class StateB(State):
def get_msg(self, w):
if 7 <= w.hour < 12:
print(f"当前时间:{w.hour},在上午")
class StateC(State):
def get_msg(self, w):
if 12 <= w.hour < 19:
print(f"当前时间:{w.hour},在下午")
class StateD(State):
def get_msg(self, w):
if 19 <= w.hour < 24:
print(f"当前时间:{w.hour},在晚上")
if __name__ == "__main__":
work = Work(status=[StateA(), StateB(), StateC(), StateD()])
work.hour = 5
work.speak()
work.hour = 11
work.speak()
work.hour = 15
work.speak()
work.hour = 21
work.speak()