forked from mlyangyue/design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate_pattern.py
78 lines (57 loc) · 1.7 KB
/
state_pattern.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
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——状态模式
状态模式(State Pattern):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类
应用场景:当控制一个对象的状态转换的条件表达式过于复杂时,把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化
(当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变他的行为)
"""
class State(object):
def __init__(self):
pass
def write_program(self, w):
pass
class Work(object):
def __init__(self):
self.hour = 9
self.curr = ForenoonState()
def set_state(self, s):
self.curr = s
def write_program(self):
self.curr.write_program(self)
class ForenoonState(State):
def write_program(self, w):
if w.hour < 12:
print "当前时间:%s点,"%w.hour,"精神百倍"
else:
w.set_state(AfternoonState())
w.write_program()
class AfternoonState(State):
def write_program(self, w):
if w.hour < 17:
print "当前时间:%s点,"%w.hour,"状态还行,继续努力"
else:
w.set_state(EveningState())
w.write_program()
class EveningState(State):
def write_program(self, w):
if w.hour < 21:
print "当前时间:%s点,"%w.hour,"加班呢,疲劳了"
else:
w.set_state(SleepState())
w.write_program()
class SleepState(State):
def write_program(self, w):
print "当前时间:%s点,"%w.hour,"不行了,睡着了"
if __name__=="__main__":
work = Work()
work.hour = 9
work.write_program()
work.hour = 15
work.write_program()
work.hour = 20
work.write_program()
work.hour = 22
work.write_program()