Created
May 28, 2012 03:45
-
-
Save sanpingz/2817115 to your computer and use it in GitHub Desktop.
应用程序框架
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Template and Command | |
import static com.mceiba.util.Print.*; | |
import java.util.*; | |
abstract class Event{ | |
private long eventTime; | |
protected final long delayTime; | |
public Event(long delayTime){ | |
this.delayTime = delayTime; | |
start(); | |
} | |
public void start(){ | |
eventTime = System.nanoTime()+delayTime; | |
} | |
public boolean ready(){ | |
return System.nanoTime()>=eventTime; | |
} | |
public abstract void action(); | |
} | |
class Controller{ | |
private List<Event> eventList = new ArrayList<Event>(); | |
public void addEvent(Event e) { eventList.add(e); } | |
public void run(){ | |
while(eventList.size()>0) | |
for(Event e: new ArrayList<Event>(eventList)) | |
if(e.ready()){ | |
println(e); | |
e.action(); | |
eventList.remove(e); | |
} | |
} | |
} | |
class GreenhouseControls extends Controller{ | |
private boolean light = false; | |
private boolean water = false; | |
private String thermostat = "Day"; | |
public class LightOn extends Event{ | |
public LightOn(long delayTime) { super(delayTime); } | |
public void action(){ | |
//hardware control code | |
//turn on the light | |
light = true; | |
} | |
public String toString() { return "Light is on"; } | |
} | |
public class LightOff extends Event{ | |
public LightOff(long delayTime) { super(delayTime); } | |
public void action(){ | |
//hardware control code | |
//turn off the light | |
light = false; | |
} | |
public String toString() { return "Light is off"; } | |
} | |
public class WaterOn extends Event{ | |
public WaterOn(long delayTime) { super(delayTime); } | |
public void action(){ | |
//hardware control code | |
//turn on the water | |
water = true; | |
} | |
public String toString() { return "Water is on"; } | |
} | |
public class WaterOff extends Event{ | |
public WaterOff(long delayTime) { super(delayTime); } | |
public void action(){ | |
//hardware control code | |
//turn off the water | |
water = false; | |
} | |
public String toString() { return "Water is off"; } | |
} | |
public class ThermostatNight extends Event{ | |
public ThermostatNight(long delayTime) { super(delayTime); } | |
public void action(){ | |
//hardware control code | |
thermostat = "Night"; | |
} | |
public String toString() { return "Thermostat is on night setting"; } | |
} | |
public class ThermostatDay extends Event{ | |
public ThermostatDay(long delayTime) { super(delayTime); } | |
public void action(){ | |
//hardware control code | |
thermostat = "Day"; | |
} | |
public String toString() { return "Thermostat is on day setting"; } | |
} | |
public class Bell extends Event{ | |
public Bell(long delayTime) { super(delayTime); } | |
public void action(){ | |
addEvent(new Bell(delayTime)); | |
} | |
public String toString() { return "Bing!"; } | |
} | |
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); | |
} | |
} | |
public void action(){ | |
for(Event e: eventList){ | |
e.start(); //Rerun each Event | |
addEvent(e); | |
} | |
start(); //Rerun this event | |
addEvent(this); | |
} | |
public String toString() { return "Restarting system"; } | |
} | |
public static class Terminate extends Event{ | |
public Terminate(long delayTime) { super(delayTime); } | |
public void action(){ | |
System.exit(0); | |
} | |
public String toString() { return "Terminating"; } | |
} | |
} | |
public class ControlFramework{ | |
public static void main(String[] args){ | |
GreenhouseControls gc = new GreenhouseControls(); | |
//Configuration | |
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(200, eventList)); | |
if(args.length == 1) | |
gc.addEvent( | |
new GreenhouseControls.Terminate( | |
new Integer(args[0]))); | |
gc.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment