-- [State Pattern is a design Pattern and a behavior Pattern. Applicable scenarios: 1. The behavior of an object depends on its status, and it must change its behavior according to its status at runtime. 2. An operation contains a large multi-branch structure, and these branches depend on the object State.] -- Work = {} function Work: new (o, s) o = o or {} setretriable (o, self) self. _ index = selfo. hour = 0o. currentstate = sreturn o; endfunction Work: SetTime (h) self. hour = Henderson function Work: GetTime () return self. hourendfunction Work: SetState (s) self. currentstate = sendfunction Work: WriteProgram () self. currentstate: WriteProgram (self) endState = {} function State: new (o) o = o or {} setretriable (o, self) self. _ index = selfreturn o; endMorningState = State: new () function MorningState: WriteProgram (w) if w. hour <12 and w. hour> = 9 thenprint ("writing code in the morning, hundred times") elsew: SetState (NoonState: new () w: WriteProgram () endendNoonState = State: new () function NoonState: writeProgram (w) if w. hour <14 and w. hour> = 12 thenprint ("code written at noon, sleepy, take a nap and then") elsew: SetState (AfternoonState: new () w: WriteProgram () endendAfternoonState = State: new () function AfternoonState: WriteProgram (w) if w. hour <17 and w. hour> = 14 thenprint ("write code in the afternoon, energetic") elsew: SetState (EveningState: new () w: WriteProgram () endendEveningState = State: new () function EveningState: writeProgram (w) if w. hour <20 and w. hour> = 17 thenprint ("writing code in the evening, a little tired, but continue") elsew: SetState (NightState: new () w: WriteProgram () endendNightState = State: new () function MAID: WriteProgram (w) if w. hour <22 and w. hour> = 20 thenprint ("writing code at night, hard work") elsew: SetState (MidnightState: new () w: WriteProgram () endendMidnightState = State: new () function MidnightState: WriteProgram (w) if w. hour <9 or w. hour> = 22 thenprint ("write code late at night, Nima, wake up and say") elsew: SetState (MorningState: new () w: WriteProgram () endendw = 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 () w: SetTime (15) w: WriteProgram () w: SetTime (23) w: WriteProgram ()