Internal class and control framework Java

Source: Internet
Author: User
Package innerclasses. controller; public abstract class event {private long eventtime; protected final long delaytime;/*** set the maximum latency * @ Param delaytime */public event (long delaytime) {This. delaytime = delaytime; Start () ;}/ *** obtain the minimum delay time when the start time */Public void start () {eventtime = system. nanotime () + delaytime;}/*** determine whether to delay * @ return */Public Boolean ready () {return system. nanotime ()> = eventtime;}/*** specific transaction */public abstract void action ();}

Package innerclasses. controller; import java. util. arrayList; import java. util. list;/*** transaction process processor * processes transactions in a certain order * @ author hehe **/public class Controller {// processes transactions in a linear order, stack can also be used for processing, that is, to process the transaction in a later order. private List <Event> eventList = new ArrayList <Event> (); /*** Add a transaction at the end of the queue * @ param c */public void addEvent (Event c) {eventList. add (c);}/*** specific transaction processing process, delete the processed transaction */public void run () {while (eventList. size ()> 0) {for (Event e: new ArrayList <Event> (eventList) {if (e. ready () {System. out. println (e); e. action (); eventList. remove (e );}}}}}
Package innerclasses; import innerclasses. controller. controller; import innerclasses. controller. event;/*** specific transaction implementation (internal class method) * @ author hehe **/public class greenhousecontrols extends controller {// you can use an internal class to access all methods and fields outside the internal class, such as the addevent method private Boolean light in Light Controller = false; /*** turn on the light event ** @ author hehe */public class lighton extends event {public lighton (long delaytime) {super (delaytime);} @ overridepu BLIC void action () {Light = true ;}@ overridepublic string tostring () {return "light is on ";}} /*** Bell event ** @ author hehe **/public class bell extends event {public Bell (long delaytime) {super (delaytime);} @ overridepublic void action () {// recursive Bell events if delaytime is the same as addevent (this); the method is the same as addevent (New Bell (delaytime) ;}@ overridepublic string tostring () {return "Bing! ";}}/*** Restart transaction * @ author hehe **/public class restart extends event {private event [] eventlist; Public restart (long delaytime, event [] eventlist) {super (delaytime); this. eventlist = eventlist; For (event E: eventlist) {addevent (e) ;}@overridepublic void action () {// put the transaction to be restarted into the queue again for (event E: eventlist) {e. start (); addevent (e) ;}// recursively restart transaction start (); addevent (this) ;}@ overridepublic string tostring () {return "Restarting system ";}} // The static internal class cannot access non-static methods and fields outside the internal class. Public static class terminate extends event {public terminate (long delaytime) {super (delaytime );} @ overridepublic void action () {system. exit (0) ;}@ overridepublic string tostring () {return "Terminating ";}} // ----------------------------------- the preceding method is similar to except // public class lightoff extends event {public lightoff (long delaytime) {super (delaytime) ;}@ overridepublic void action () {Light = false ;}@ overridepublic string tostring () {// todo auto-generated method stubreturn "light is off" ;}} private Boolean water = false; public class wateon extends event {public wateon (long delaytime) {super (delaytime) ;}@ overridepublic void action () {water = true ;}@ overridepublic string tostring () {return "water is on" ;}} public class wateroff extends event {public wateroff (long delaytime) {super (delaytime) ;}@ overridepublic void action () {water = false ;}@ overridepublic string tostring () {return "water is off" ;}} private string thermostat = "day "; public class thermostatnight extends event {public thermostatnight (long delaytime) {super (delaytime) ;}@ overridepublic void action () {thermostat = "night" ;}@ overridepublic string tostring () {return "thermostat on night setting" ;}} public class thermostatday extends event {public thermostatday (long delaytime) {super (delaytime) ;}@ overridepublic void action () {thermostat = "day" ;}@ overridepublic string tostring () {return "thermostat on day setting ";}}}
Package innerclasses; import innerclasses. controller. event; public class greenhousecontroller {/** Test Method * @ Param ARGs */public static void main (string [] ARGs) {greenhousecontrols GC = new greenhousecontrols (); GC. addevent (GC. new Bell (900); event [] eventlist = {GC. new thermostatnight (0), GC. new lighton (200), GC. new lightoff (200), GC. new wateon (600), GC. new wateroff (800), GC. new thermostatday (1400)}; // repeat the Method GC in event. addevent (GC. new Restart (2000, eventlist); // sets the maximum running event of the program running (this method will be infinitely executed if it is not set) if (ARGs. length = 1) {GC. addevent (New greenhousecontrols. terminate (New INTEGER (ARGs [0]);} // run the transaction queue GC. run ();}}

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.