控制温室的运作代码分析
1、下面的控制温室的类的父类-->Controller类
这是一个基类
// The reusable framework for control systems.
package com.chapter10.controller;
import java.util.*;
public class Controller {
// A class from java.util to hold Event objects:
private List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c) {
eventList.add(c);
}
public void run() {
while (eventList.size() > 0)
// Make a copy so you're not modifying the list
// while you're selecting the elements in it:
for (Event e : new ArrayList<Event>(eventList)) {
System.out.println(e);
if (e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
}
} ///:~
2、下面的控制温室程序的内部类的父类-->Event类
// The common methods for any control event.
package com.chapter10.controller;
// 默认的行为是基于时间取执行控制
public abstract class Event {
private long eventTime;
protected final long delayTime; // 延迟时间
public Event(long delayTime) {
this.delayTime = delayTime;
start();
}
public void start() { // Allows restarting
eventTime = System.nanoTime() + delayTime;
}
public boolean ready() {
System.out.println("===================>debug");
System.out.println(System.nanoTime());
System.out.println(eventTime);
System.out.println("===================>debug");
return System.nanoTime() >= eventTime;
}
public abstract void action();
} ///:~
3、应用程序框架,GreenhouseControls类继承自Controller
package com.chapter10;
// This produces a specific application of the
// control system, all in a single class. Inner
// classes allow you to encapsulate different
// functionality for each type of event.
import com.chapter10.controller.*;
public class GreenhouseControls extends Controller {
private boolean light = false;
public class LightOn extends Event {
public LightOn(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here to
// physically 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() {
// Put hardware control code here to
// physically turn off the light.
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() {
// Put hardware control code here.
water = true;
}
public String toString() {
return "Greenhouse water is on";
}
}
public class WaterOff extends Event {
public WaterOff(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here.
water = false;
}
public String toString() {
return "Greenhouse water is off";
}
}
private String thermostat = "Day";
public class ThermostatNight extends Event {
public ThermostatNight(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here.
thermostat = "Night";
}
public String toString() {
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event {
public ThermostatDay(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here.
thermostat = "Day";
}
public String toString() {
return "Thermostat on day setting";
}
}
// An example of an action() that inserts a
// new one of itself into the event list:
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";
}
}
} ///:~
4、通过创建一个GreenhouseControls对象,并添加各种不同的Event对象来配置该系统。
package com.chapter10;
// Configure and execute the greenhouse system.
// {Args: 5000}
import com.chapter10.controller.*;
public class GreenhouseController {
public static void main(String[] args) {
GreenhouseControls gc = new GreenhouseControls();
// Instead of hard-wiring, you could parse
// configuration information from a text file here:
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(
Integer.valueOf(args[0])));
gc.run();
}
} /* Output:
Bing!
Thermostat on night setting
Light is on
Light is off
Greenhouse water is on
Greenhouse water is off
Thermostat on day setting
Restarting system
Terminating
*///:~
4、笔记
4.1、这里要注意,就像书中所说的那样,对,要注意Bell和Restart这两个类。
- Bell类,这个类在调用的时候,也就是执行它的action方法时,会给GreenhouseControls这个类的eventList集合新增一个Bell,以便下一次循环执行这些控制方法的时候能够再次执行自身的方法。(注意,eventList这个字段是在GreehouseControls的父类Controller中,因此可以自由地使用)
- Restart类:这个类在一开始调用的时候
gc.addEvent(gc.new Restart(2000, eventList));
是预先存了一个eventList的,所以这个类在执行action方法的时候,会把eventList数组中的数据添加到外围类的eventList中,本来在Restart这个类实例之前的数据都已经被remove掉了,但是通过这个方法,又将前面的除了Bell的其他的类实例又添加了进来(Bell是自己把自己又添加了一次),然后Restart又通过addEvent(this);
把自己又添加了进去,如此往复,循环调用。
4.2、注意一个细节
书中源码有一处,就是遍历集合的时候,使用了这样一段代码:
for (Event e : new ArrayList<Event>(eventList))
if (e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
这里因为内部只有一块代码,所以程序没有使用花括号,可是debug的时候可能会使人困惑,就是在IDEA中它的if语句判断得执行有些问题,虽然不影响最后的结果。所以这里加上花括号的话,单步执行会看得更加清晰一点。
4.3、关于delayTime的设置
我认为这些delayTime的设置,因为是纳秒,在java中,System.nanoTime()
是获取系统时间的纳秒表示的意思,这个数量级太小,所以基本上数据在eventList中的顺序就是最后的执行顺序,可能以后通过设置大小可以调节一些控制设置的行为,估计就是后面的从文件中读取配置。
4.4、关于在控制台执行这个程序
因为这个程序包含了其他的类,甚至还有其他包的类,所以在控制台进行编译运行就需要新的方法。但是找到了更简单的方法,直接在IDEA中给main函数传参:
我就说这么智能的IDEA怎么会没有给main函数传参的方式呢,原来只是我见识太少罢了😂。
传好参数之后,就能够运行出书中的结果了,而不是无休止地无限循环下去: