--------------------- android培训、java培训、期待与您交流! ----------------------
一丶分析与设计
路线图:
一丶分析与设计
1、对象:红绿灯,红绿灯控制系统,汽车,路线
|--因为不需要体现车的移动过程,只是捕捉车辆穿过路口的过程,所以不需要单独设计为一个对象,
|--用字符串表示就可以了
2、总共有12条路线,设计一个Road类来表示路线,则有12个Road实例对象
3、每条路线都会随机增加车辆,则用集合保存
4、每条路线每个一秒都会检查控制本路线的灯是否为绿,一个灯由绿变红时,应该将下一个方向的灯变绿
|--设计一个Lamp类表示一个交通灯,每个交通灯都维护一个状态:亮(绿)或者不亮(红)
|--每个交通灯都要有变亮和变黑的方法,而且能返回自己的亮黑状态
|--总共有12条路线,所以,系统中总共要产生12个交通灯,右拐弯的路线本来不受灯的控制,但是为了让程序采用统一的处理方式
|--故假设有4个右拐弯的灯,只是这些灯为常亮状态
|--除了右拐弯方向的其他8条路线,它们是两两成对的,可以归为4组
|--无论在程序什么地方去获得某个方向的灯时,每次获取的都是同一个实例对象,所以Lamp类使用枚举更方便
|--设计一个LampController类,它定时让当前的绿灯变红
5、Road类-路
package study_note_traffic;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/*
*路
*/
public class Road
{
private List vechicles = new ArrayList();
private String name = null;
public Road(String name)
{
this.name = name;
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(new Runnable()
{
public void run()
{
for (int i = 1; i < 1000; i++)
{
try
{
Thread.sleep((new Random().nextInt(10) + 1) * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
vechicles.add(Road.this.name + "_" + i);
}
}
});
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable()
{
public void run()
{
if (vechicles.size() > 0)
{
boolean lighted = Lamp.valueOf(Road.this.name).isLighted();
if (lighted)
{
System.out.println(vechicles.remove(0) + " is traversing !");
}
}
}
}, 1, 1, TimeUnit.SECONDS);
}
}
package study_note_traffic;
/*
*灯
*/
public enum Lamp
{
//
S2N("N2S", "S2W", false), S2W("N2E", "E2W", false), E2W("W2E", "E2S", false), E2S("W2N", "S2N", false),
//
N2S(null, null, false), N2E(null, null, false), W2E(null, null, false), W2N(null, null, false),
//
S2E(null, null, true), E2N(null, null, true), N2W(null, null, true), W2S(null, null, true);
private Lamp(String opposite, String next, boolean lighted)
{
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
private String next;
private boolean lighted;
private String opposite;
public boolean isLighted()
{
return lighted;
}
// 变绿
public void light()
{
this.lighted = true;
if (opposite != null)
{
Lamp.valueOf(opposite).light();
}
System.out.println(name() + " bian绿了");
}
// 变黑
public Lamp blackOut()
{
this.lighted = false;
if (opposite != null)
{
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp = null;
if (next != null)
{
nextLamp = Lamp.valueOf(next);
Lamp.valueOf(next).light();
System.out.println("绿灯从 " + name() + " ---->切换为 " + next);
}
return nextLamp;
}
}
package study_note_traffic;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/*
*控制器
*/
public class LampController
{
private Lamp currentLamp;
public LampController()
{
currentLamp = Lamp.S2N;
currentLamp.light();
// 做个定时器.每隔一段时间控制灯和下个灯的颜色
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable()
{
public void run()
{
System.out.println("变灯了");
currentLamp = currentLamp.blackOut();
}
}, 10, 10, TimeUnit.SECONDS);
}
}
--------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima