- 应用程序框架就是被设计用以解决某类特定问题的一个类或一组类。要运用某个应用程序框架,通常是继承一个或多个类,并覆盖某些方法。在覆盖后的方法中,编写代码定制应用程序框架提供的通用解决方案。
- 内部类允许:
①控制框架的完整实现是由单个的类创建的,从而实现的细节被封装了起来。内部类用来表示解决问题所必须的各种不同的action。
②内部类能够很容易地访问外围类的任意成员,所以可以避免这种实现变得笨拙。
package com2002;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Panda on 2018/6/11.
*/
public class Controller {
private List<Event> eventList=new ArrayList<>();
public void addEvent(Event event){eventList.add(event);}
public void run(){
while (eventList.size()>0){
for(Event event :new ArrayList<>(eventList)){
if(event.ready()){
System.out.println(event);
event.action();
eventList.remove(event);
}
}
}
}
}
package com2002;
/**
* Created by Panda on 2018/6/11.
*/
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();
}
package com2002;
/**
* Created by Panda on 2018/6/11.
*/
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;
}
public String toString(){return "Light is on";}
}
public class LightOff extends Event{
public LightOff(long delayTime){super(delayTime);}
@Override
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);}
@Override
public void action() {
water=true;
}
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;
}
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";
}
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";
}
public String toString(){
return "Thermostat on day setting";
}
}
public class Bell extends Event{
public Bell(long delayTime){super(delayTime);}
@Override
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 event:eventList)
addEvent(event);
}
@Override
public void action() {
for(Event event:eventList){
event.start();
addEvent(event);
}
start();
addEvent(this);
}
public String toString(){return "Restarting system";}
}
public static class Terminate extends Event{
public Terminate(long delayTime){super(delayTime);}
@Override
public void action() {
System.exit(0);
}
public String toString(){
return "Terminating";
}
}
}
package com2002;
/**
* Created by Panda on 2018/6/11.
*/
public class GreenhouseController {
public static void main(String[] args) {
GreenhouseControls greenhouseControls = new GreenhouseControls();
greenhouseControls.addEvent(greenhouseControls.new Bell(900));
Event[] events={greenhouseControls.new ThermostatNight(0),greenhouseControls.new LightOn(200),
greenhouseControls.new LightOff(400),greenhouseControls.new WaterOn(600),
greenhouseControls.new WaterOff(800),greenhouseControls.new ThermostatDay(1400)};
greenhouseControls.addEvent(greenhouseControls.new Restart(2000,events));
if(args.length==1){
greenhouseControls.addEvent(new GreenhouseControls.Terminate(new Integer(args[0])));
}
greenhouseControls.run();
}
}