"Thinkinginjava" 19, the realization of control framework

Source: Internet
Author: User

/*** book: "Thinking in Java" * Function: Control Framework Implementation * File: event.java* time: April 3, 2015 21:33:11* Author: cutter_point*/package Lession10innerclasses.controller;public Abstract class Event {Private long eventtime;protected final long delaytime; Public Event (Long delaytime) {this.delaytime = Delaytime;this.start ();} public void Start () {eventtime = System.nanotime () + delaytime;} public Boolean Ready () {return system.nanotime () >= eventtime;//exceeds the delay time}public abstract void Action ();}


/*** book: "Thinking in Java" * Function: Control Framework Implementation * File: controller.java* time: April 3, 2015 21:38:38* Author: cutter_point*/package Lession10innerclasses.controller;import Java.util.arraylist;import Java.util.list;public class Controller {private list<event> eventlist = new arraylist<event> ();p ublic void addevent (Event c) {Eventlist.add (c);} public void Run () {while (eventlist.size () > 0) {for (Event e:new arraylist<event> (eventlist))//Make a copy of EventList To avoid altering the original data if (E.ready ()) {System.out.println (e); e.action (); Eventlist.remove (e);}}}

/*** book: "Thinking in Java" * Function: Control framework implementation, 1, the complete implementation of the control framework is created by a single class, so that the implementation of the details are encapsulated. The inner class is used to represent the various action* necessary to solve the problem, and 2, the inner class can easily access any member of the perimeter class, so it is possible to avoid this implementation becoming unwieldy. * File: greenhousecontrols.java* time: April 7, 2015 18:29:47* Author: cutter_point*/package Lession10innerclasses;import lession10innerclasses.controller.*;p ublic class Greenhousecontrols extends Controller{private Boolean light = false; public class Lighton extends event//because Event is an abstract class, you must implement constructors and abstract functions {public Lighton (long Delaytime) {super (delaytime);} @Overridepublic void Action () {light = true;} Public String toString () {return ' light was on ';}} public class Lightoff extends event//because Event is an abstract class, you must implement constructors and abstract functions {public lightoff (long Delaytime) {super (delaytime);} @Overridepublic void Action () {light = false;} Public String toString () {return ' light is Off ';}} Private Boolean water = False;public class Wateron extends event//because Event is an abstract class, you must implement constructors and abstract functions {public Wateron (long Delaytime) {super (delaytime);} @Overridepublic void Action () {water = true;} Public String Tostring () {return "Greenhouse is on";}} public class Wateroff extends event//because Event is an abstract class, you must implement constructors and abstract functions {public wateroff (long Delaytime) {super (delaytime);} @Overridepublic void Action () {water = false;} Public String toString () {return ' greenhouse is off ';}} Private String thermostat = "Day";p Ublic class Thermostatday extends event//because Event is an abstract class, you must implement constructors and abstract functions {public Thermostatday (Long Delaytime) {super (delaytime);} @Overridepublic void Action () {thermostat = "Day";} Public String toString () {return ' thermostat on day setting ';}} public class Thermostatnight extends event//because Event is an abstract class, you must implement constructors and abstract functions {public thermostatnight (long Delaytime) {Super (Delaytime);} @Overridepublic void Action () {thermostat = "Night";} Public String toString () {return ' thermostat on Night Setting ";}} public class Bell extends event//because the Event is an abstract class, you must implement the constructor and abstract function {public Bell (long Delaytime) {super (delaytime);} @Overridepublic void Action () {greenhousecontrols:addevent (new Bell (Delaytime));//This is his father.}public String toStRing () {return "bing!!";}} public class Restart extends event//because Event is an abstract class, you must implement constructors and abstract functions {private event[] eventlist;public Restart (long Delaytime, event[] eventlist) {super (delaytime); this.eventlist = Eventlist;for (Event e:eventlist) Greenhousecontrols: Addevent (e);} @Overridepublic void Action () {for (Event e:eventlist) {E.start (); Greenhousecontrols:addevent (e);} This.start (); Greenhousecontrols:addevent (this);} Public String toString () {return ' restarting system ';}} public static class Terminate extends Event{public Terminate (long delaytime) {super (delaytime);} @Overridepublic void Action () {system.exit (0);} Public String toString () {return ' terminating ';}}}

Output:

Other Operation Obj1
1 obj1
------------------------
1 obj1
2 obj1
------------------------
Other Operation Obj1
2 obj1
Other Operation Obj1
3 obj1



Sometimes it will feel very painful, I TM write n more code, the results of the implementation is only a little bit, is it really concentrated is the essence???






"Thinkinginjava" 19, the realization of control framework

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.