【ThinkingInJava】19、控制框架的实现

/**
* 书本:《Thinking In Java》
* 功能:控制框架的实现
* 文件:Event.java
* 时间:2015年4月3日21:33:11
* 作者:cutter_point
*/
package Lession10innerclasses.controller;

public abstract class Event 
{
	private long eventTime;
	protected final long delayTime;
	public Event(long delayTime)
	{
		this.delayTime = delayTime;
		this.start();
	}
	
	public void start() 
	{
		eventTime = System.nanoTime() + delayTime;
	}
	
	public boolean ready()
	{
		return System.nanoTime() >= eventTime;	//超过了延迟时间
	}
	
	public abstract void action();
}


/**
* 书本:《Thinking In Java》
* 功能:控制框架的实现
* 文件:Controller.java
* 时间:2015年4月3日21:38:38
* 作者:cutter_point
*/
package Lession10innerclasses.controller;

import java.util.ArrayList;
import java.util.List;

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))	//对eventList做一个拷贝,免得改动了原来的数据
				if(e.ready())
				{
					System.out.println(e);
					e.action();
					eventList.remove(e);
				}
		}
	}
}

/**
* 书本:《Thinking In Java》
* 功能:控制框架的实现,1、控制框架的完整实现是由单个的类创建的,从而使得实现的细节被封装了起来。内部类用来表示解决问题所必须的各种不同的action
* 				    ,2、内部类能够很容易地访问外围类的任意成员,所以可以避免这种实现变得笨拙。
* 文件:GreenhouseControls.java
* 时间:2015年4月7日18:29:47
* 作者:cutter_point
*/
package Lession10innerclasses;

import Lession10innerclasses.controller.*;

public class GreenhouseControls extends Controller
{
	private boolean light = false;
	
	public class LightOn extends Event		//由于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		//由于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		//由于event是抽象类,所以必须实现构造函数和抽象函数
	{

		public waterOn(long delayTime) 
		{
			super(delayTime);
		}

		@Override
		public void action() 
		{
			water = true;
			
		}
		
		public String toString() { return "Greenhouse is on"; }
		
	}
	
	public class waterOff extends Event		//由于event是抽象类,所以必须实现构造函数和抽象函数
	{

		public waterOff(long delayTime) 
		{
			super(delayTime);
		}

		@Override
		public void action() 
		{
			water = false;
			
		}
		
		public String toString() { return "Greenhouse is off"; }
		
	}
	
	private String thermostat = "Day";
	
	public class ThermostatDay extends Event		//由于event是抽象类,所以必须实现构造函数和抽象函数
	{

		public ThermostatDay(long delayTime) 
		{
			super(delayTime);
		}

		@Override
		public void action() 
		{
			thermostat = "Day";
			
		}
		
		public String toString() { return "Thermostat on day setting"; }
		
	}
	
	public class ThermostatNight extends Event		//由于event是抽象类,所以必须实现构造函数和抽象函数
	{

		public ThermostatNight(long delayTime) 
		{
			super(delayTime);
		}

		@Override
		public void action() 
		{
			thermostat = "Night";
			
		}
		
		public String toString() { return "Thermostat on night setting"; }
		
	}
	
	public class Bell extends Event		//由于event是抽象类,所以必须实现构造函数和抽象函数
	{

		public Bell(long delayTime) 
		{
			super(delayTime);
		}

		@Override
		public void action() 
		{
			GreenhouseControls:addEvent(new Bell(delayTime));	//这个也算是他的父类
			
		}
		
		public String toString() { return "Bing!!"; }
		
	}
	
	public class Restart extends Event		//由于event是抽象类,所以必须实现构造函数和抽象函数
	{
		private Event[] eventList;

		public Restart(long delayTime, Event[] eventList) 
		{
			super(delayTime);
			this.eventList = eventList;
			for(Event e : eventList)
				GreenhouseControls:addEvent(e);
			
		}

		@Override
		public void action() 
		{
			for(Event e : eventList)
			{
				e.start();
				GreenhouseControls:addEvent(e);
				
			}
			this.start();
			GreenhouseControls: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";
		}		
	}
}

输出:

Other operation  obj1
1  obj1
------------------------
1  obj1
2  obj1
------------------------
Other operation  obj1
2  obj1
Other operation  obj1
3  obj1



有的时候会觉得很蛋疼,我TM写了N多代码,执行的结果却只有那么一点点,难道真的是浓缩的是精华???






郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。