事件的框架:
public 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();
}
控制器的类
import java.util.*;
public class Controller
{
private List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c)
{
eventList.add(c);
}
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);
}
}
}
}
}
温室的控制类
public class GreenhouseControls extends Controller
{
//灯光系统部分
private boolean light = false;
//控制灯光系统的内部类
//开灯内部类
public class LightOn extends Event
{
public LightOn (long delayTime)
{
super(delayTime);
}
public void action()
{
//打开电灯
light = true;
}
public String toString()
{
return "Light is on";
}
}
//关灯内部类
public class LightOff extends Event
{
public LightOff (long delayTime)
{
super(delayTime);
}
public void action()
{
//关闭电灯
light = false;
}
public String toString()
{
return "Light is off";
}
}
//水利部分
private boolean water = false;
public class WaterOn extends Event
{
public WaterOn (long delayTime)
{
super(delayTime);
}
public void action()
{
water = true;
}
public String toString()
{
return "Water is on";
}
}
public class WaterOff extends Event
{
public WaterOff (long delayTime)
{
super(delayTime);
}
public void action()
{
water = false;
}
public String toString()
{
return "Water is off";
}
}
//温度控制
private String thermostat = "Day";
public class ThermostatNight extends Event
{
public ThermostatNight(long delayTime)
{
super(delayTime);
}
public void action()
{
thermostat = "Night";
}
public String toString()
{
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event
{
public ThermostatDay(long delayTime)
{
super(delayTime);
}
public void action()
{
thermostat = "Day";
}
public String toString()
{
return "Thermostat 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();// Return each event
addEvent(e);
}
start();//Return 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 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(new GreenhouseControls.Terminate(new Integer(args[0])));
}
gc.run();
}
}
输出
Bing
Thermostat on night setting
Light is on
Light is off
Water is on
Water is off
Thermostat on Day setting
Restarting system
Terminating