Status mode lua implementation

Source: Internet
Author: User

-- [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 ()

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.