-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathState.py
More file actions
111 lines (83 loc) · 2.15 KB
/
State.py
File metadata and controls
111 lines (83 loc) · 2.15 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @Time : 2022/08/29 13:47:07
# @Author : Gary
# @Email : None
class LiftState:
# 抽象状态类
def open(self):
pass
def close(self):
pass
def run(self):
pass
def stop(self):
pass
class OpenState(LiftState):
# 具体状态类
def open(self):
print("OPEN:The door is opened...")
return self
def close(self):
print("OPEN:The door start to close...")
print("OPEN:The door is closed")
return StopState()
def run(self):
print("OPEN:Run Forbidden.")
return self
def stop(self):
print("OPEN:Stop Forbidden.")
return self
class RunState(LiftState):
# 具体状态类
def open(self):
print("RUN:Open Forbidden.")
return self
def close(self):
print("RUN:Close Forbidden.")
return self
def run(self):
print("RUN:The lift is running...")
return self
def stop(self):
print("RUN:The lift start to stop...")
print("RUN:The lift stopped...")
return StopState()
class StopState(LiftState):
# 具体状态类
def open(self):
print("STOP:The door is opening...")
print("STOP:The door is opened...")
return OpenState()
def close(self):
print("STOP:Close Forbidden")
return self
def run(self):
print("STOP:The lift start to run...")
return RunState()
def stop(self):
print("STOP:The lift is stopped.")
return self
class Context:
# 环境类
lift_state = ""
def getState(self):
return self.lift_state
def setState(self, lift_state):
self.lift_state = lift_state
def open(self):
self.setState(self.lift_state.open())
def close(self):
self.setState(self.lift_state.close())
def run(self):
self.setState(self.lift_state.run())
def stop(self):
self.setState(self.lift_state.stop())
if __name__ == "__main__":
ctx = Context()
ctx.setState(StopState())
ctx.open()
ctx.run()
ctx.close()
ctx.run()
ctx.stop()