public abstract class Event {
private long eventTime;
protected final long delayTime;
public Event(long delayTime){
this.delayTime=delayTime;
}
public void start(){
eventTime=System.currentTimeMillis()+delayTime;
}
public boolean ready(){
return System.currentTimeMillis()>=eventTime;
}
public abstract void action();
}
//------------------------------------------------------------------------------------------------------------
package com.thinkinjava.framework;
import java.util.ArrayList;
import java.util.List;
public 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(int i=0;i<eventList.size();++i){
Event e=(Event) eventList.get(i);
if(e.ready()){
System.out.println(e);
e.action();
eventList.remove(i);
}
}
}
}
public Controller() {
}
}
//-----------------------------------------------------------------------------------------------------
package com.thinkinjava.framework;
public class GreenHouseControls extends Controller{
private boolean light=false;
public class LightOn extends Event{
public LightOn(long delayTime) {
super(delayTime);
}
@Override
public void action() {
light=true;
}
@Override
public String toString() {
return "Light is on!";
}
}
public class LightOff extends Event{
public LightOff(long delayTime) {
super(delayTime);
}
@Override
public void action() {
light=false;
}
@Override
public String toString() {
return "Light is off!";
}
}
private boolean water = false;
public class WaterOn extends Event{
public WaterOn(long delayTime) {
super(delayTime);
}
@Override
public void action() {
water=true;
}
@Override
public String toString() {
return "Greenhouse water is on!";
}
}
public class WaterOff extends Event{
public WaterOff(long delayTime) {
super(delayTime);
}
@Override
public void action() {
water = false;
}
@Override
public String toString() {
return "Greenhouse water is off!";
}
}
private String thermostat ="Day";
public class ThermostatNight extends Event{
public ThermostatNight(long delayTime) {
super(delayTime);
}
@Override
public void action() {
thermostat ="Night";
}
@Override
public String toString() {
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event{
public ThermostatDay(long delayTime) {
super(delayTime);
}
@Override
public void action() {
thermostat ="Day";
}
@Override
public String toString() {
return "Thermostat on night setting";
}
}
public class Bell extends Event{
public Bell(long delayTime) {
super(delayTime);
}
@Override
public void action() {
addEvent(new Bell(delayTime));
}
@Override
public String toString() {
return "Bing!";
}
}
public class Restart extends Event{
private Event[] eventList;
public Restart(long delayTime,Event[] eventList) {
super(delayTime);
this.eventList=eventList;
}
@Override
public void action() {
for(int i=0;i<eventList.length;++i){
eventList[i].start();
addEvent(eventList[i]);
}
start();
addEvent(this);
}
@Override
public String toString() {
return "Restarting system!";
}
}
public class Terminate extends Event{
public Terminate(long delayTime) {
super(delayTime);
}
@Override
public void action() {
System.exit(0);
}
@Override
public String toString() {
return "Terminating";
}
}
}
//-------------------------------------------------------------------------------------------------
package com.thinkinjava.framework;
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==1){
gc.addEvent(gc.new Terminate(Integer.parseInt(args[0])));
}
gc.run();
}
}