Java programming ideas: better understanding of the value of internal classes (a simple example)

Source: Internet
Author: User

Why do internal classes need to be connected to Java programming ideas?

public abstract class Event{   private long eventTime;   protected final long delayTime;   public Event(long delayTime){      this.delayTime = delayTime;      start();   }   public void start(){      eventTime = System.currentTimeMillis() + delayTime;   }   public boolean ready(){      return System.currentTimeMillis() >= eventTime;   }   public abstract void action();}

Start () is an independent method that is not included in the constructor, because it can restart the counter after the time is run, that is, the event object can be reused. For example, if you want to repeat an event, you only need to simply call the START () method in Action.

public class Controller {   private List<Object> eventList = new ArrayList<Object>();   public void addEvent(Eventevent){      eventList.add(event);   }   public void run(){      while(eventList.size() > 0){        for(int i = 0; i < eventList.size(); i++){           Event event = (Event)eventList.get(i);           if(event.ready()){              System.out.println(event);              event.action();              eventList.remove(i);           }        }      }   }}

ControllerContains an actual control framework used to manage and trigger events.

The key to design is to separate changing things from unchanged things. "Changed things" refer to different actions of different event objects, which can be realized through different event subclasses.

This is exactly what internal classes need to do. Internal classes allow:

1). Use a single class to fully implement the control framework, so as to encapsulate the implementation details. Internal classes are used to indicate various actions () not allowed to solve the problem ().

2). The internal class can easily access any member of the peripheral class and has great flexibility.

Public class greenhousecontrols extends controller {private Boolean light = false; // The default light is turned off. Private Boolean water = false; // The default water source is closed. Private string thermostat = "day"; // The default value is daytime. // The Light-on Event public class lighton extends event {public lighton (long delaytime) {super (delaytime );} public void action () {Light = true;} Public String tostring () {return "light is on ";}} // turn off the light event public class lightoff extends event {public lightoff (long delaytime) {super (delaytime) ;}public void action () {Light = false;} Public String tostring () {return "light is off" ;}// boiled water Event public class wateon extends event {public wateon (long delaytime) {super (delaytime);} public void action () {water = true;} Public String tostring () {return "water is on" ;}}// water disconnection event public class wateroff extends event {public wateroff (long delaytime) {super (delaytime);} public void action () {water = false;} Public String tostring () {return "water is off" ;}}// temperature adjustment at night event public class thermostatnight extends Event {public thermostatnight (long delaytime) {super (delaytime);} public void action () {thermostat = "night";} Public String tostring () {return "therostat on night setting" ;}// the temperature is adjusted to the daytime event public class thermostatday extends event {public thermostatday (long delaytime) {super (delaytime );} public void action () {thermostat = "day";} Public String tostring () {return "therostat on day setting" ;}}// public C Lass Bell extends event {public Bell (long delaytime) {super (delaytime);} // after the bell rings, add a new bell (delaytime) to eventlistpublic void action () {addevent (New Bell (delaytime);} Public String tostring () {return "Bing! ";}}// Restart the public class restart extends event {private event [] eventlist; Public restart (long delaytime, event [] eventlist) {super (delaytime); this. eventlist = eventlist; For (INT I = 0; I <eventlist. length; I ++) {addevent (eventlist [I]) ;}} public void action () {for (INT I = 0; I <eventlist. length; I ++) {eventlist [I]. start (); // restart each event. Addevent (eventlist [I]);} start (); // start the current restart event addevent (this); // Add the current restart event to eventlist and start it cyclically .} Public String tostring () {return "Restarting system! ";}}// Terminate the event public class terminate extends event {public terminate (long delaytime) {super (delaytime);} public void action () {system. exit (0);} Public String tostring () {return "Terminating! ";}}}

Create a greenhousecontrols object (an example of the "command" design mode ):

public class GreenhouseController {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(400),gc.new WaterOn(600),gc.new WaterOff(800),gc.new ThermostatDay(1400)};gc.addEvent(gc.new Restart(2000, eventList));if(args.length == 0){gc.addEvent(gc.new Terminate(5000));}gc.run();}}

Running result:


The above content is compiled from Java programming ideas. If any omission exists, please leave it blank!

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.