简易框架 think in java

package com.thinkinjava.framework;


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();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值