forked from woshihuo12/LuaDesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.lua
126 lines (98 loc) · 2.22 KB
/
state.lua
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
112
113
114
115
116
117
118
119
120
121
122
123
--[[
2. 状态模式(State Pattern)是设计模式的一种,属于行为模式。
3. 适用场景:
4. 1.一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。
5. 2.一个操作中含有庞大的多分支结构,并且这些分支决定于对象的状态。
6.
7. ]]
Work = {}
function Work:new(o,s)
o = o or {}
setmetatable(o, self)
self.__index = self
o.hour = 0
o.currentstate = s
return o
end
function Work:SetTime(h)
self.hour = h
end
function Work:GetTime()
return self.hour
end
function Work:SetState(s)
self.currentstate = s
end
function Work:WriteProgram()
self.currentstate:WriteProgram(self)
end
State = {}
function State:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
return o;
end
MorningState = State:new()
function MorningState:WriteProgram(w)
if w.hour < 12 and w.hour >= 9 then
print("早上写代码,精神百倍")
else
w:SetState(NoonState:new())
w:WriteProgram()
end
end
NoonState = State:new()
function NoonState:WriteProgram(w)
if w.hour < 14 and w.hour >= 12 then
print("中午写代码,犯困了,先小睡一下再说")
else
w:SetState(AfternoonState:new())
w:WriteProgram()
end
end
AfternoonState = State:new()
function AfternoonState:WriteProgram(w)
if w.hour < 17 and w.hour >= 14 then
print("下午写代码, 精神充足")
else
w:SetState(EveningState:new())
w:WriteProgram()
end
end
EveningState = State:new()
function EveningState:WriteProgram(w)
if w.hour < 20 and w.hour >=17 then
print("傍晚写代码,有点累,但还是继续")
else
w:SetState(NightState:new())
w:WriteProgram()
end
end
NightState = State:new()
function NightState:WriteProgram(w)
if w.hour < 22 and w.hour >= 20 then
print("晚上写代码,苦逼啊")
else
w:SetState(MidnightState:new())
w:WriteProgram()
end
end
MidnightState = State:new()
function MidnightState:WriteProgram(w)
if w.hour < 9 or w.hour >= 22 then
print("深夜写代码,尼玛,先睡醒了再说")
else
w:SetState(MorningState:new())
w:WriteProgram()
end
end
w = Work:new(nil,MorningState:new())
w:SetTime(21)
w:WriteProgram()
w:SetTime(18)
w:WriteProgram()
w:SetTime(10)
w:WriteProgram()
w:SetTime(13)
w:WriteProgram()