交通系统笔记
需求分析:因为是十字路口所以是有12中不同线路的车辆,为了简化思想,可以假设向右转的灯是常绿状态。
建立模型,按南北为一组,东西一组,只考虑直行和左转的情况,所以只有两条线路。
面向对象的设计与分析:
对象:灯的控制系统,汽车,路线;
汽车看到自己线路所对应的等变绿就会走:
此时路线这个对象应该提供车辆加减的方法;
汽车提供走的方法;
灯控系统提供灯的变化方法;
面向对象设计的一个重要经验:谁拥有数据,就应该对外提供操作数据的方法
其实在设计与分析的时候,经常会用到23中设计模式。
详细设计:
创建4个类:Lamp,LampContorller,Road,MainClass。
1.Lamp的类的属性和方法(Lamp是用的枚举!)
属性:boolean light
String next;
String opposite;
方法:public boolean isLigthed();判断是否是绿灯
public void light() ;
public Lamp blackout()
2.LampContorller的属性和方法
属性:private Lamp currentLamp
方法:public LampContorller()
虽然说只有一个初始化方法,但是初始化方法里面有东西,定时变化灯的状态,用到了定时器。
3.Road的属性和方法:
属性:private String name 定义路的名字
private List <String > vechicles 用一个集合来表示一起有多少车辆。
方法:public Road(String name);同样只有一个初始化方法,但是初始化里面有出现Java5的新技术,线程池的调度,分1到10s驾一辆车,每一秒走一辆车。
4.MainClass的方法和属性
属性:String [] directions=new String[]
{"S2N","S2W","E2W","E2S","N2S","N2E","W2E",
"W2N","E2N","N2W","W2S","S2E"};
12条线路
方法,只有一个main方法,主要是承担程序的入口作用。
下面是我对代码的理解
package com.isoftstone.interveiw.traffic;
public enum Lamp {
//枚举元素必须是放在所有成员元素的前面
//这些枚举元素是 调用的带参数的构造方法
S2N("N2S","S2W",false), S2W("N2E","E2W",false), E2W("W2E","E2S",false), E2S("W2N","W2N",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 boolean lighted;
private String opposite =null;
private String next;
private Lamp(){}
private Lamp(String opposite ,String next,boolean lighted){
this.opposite=opposite;//对应的等
this.next=next;//定义下一路灯
this.lighted=lighted;//定义状态
}
public boolean isLigthed() {
return lighted;
}
public void light() {
this.lighted = true;
if (opposite != null) {
Lamp.valueOf(opposite).light();
}
System.out.println(name()+" is green");
}
public Lamp blackout() {
this.lighted = false;
if (opposite != null) {
Lamp.valueOf(opposite).blackout();
}
Lamp nextLamp=null;
if(next!=null){
nextLamp=Lamp.valueOf(next);
nextLamp.light();//同时将下一路灯变绿
}
return nextLamp;
}
}
package com.isoftstone.interveiw.traffic;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
//路灯控制系统
public class LampContorller {
//当前路灯:
private Lamp currentLamp;
public LampContorller(){
currentLamp=Lamp.S2N;//调用枚举元素
currentLamp.light();
//JDK1.5以后版本的新方法,线程池调度
ScheduledExecutorService timer=Executors.newSingleThreadScheduledExecutor();
//定时器
timer.scheduleAtFixedRate(
new Runnable() {//内部类
@Override
public void run() {
currentLamp =currentLamp.blackout();
}
},
10,
10,
TimeUnit.SECONDS);
}
}
package com.isoftstone.interveiw.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 String name=null;
private List <String > vechicles=new ArrayList<String>();
//这里写List<String>而不是ArrayList<>,是因为面向接口的,更能体现技术。
public Road(String name){
this.name=name;
//java5的线程库:
ExecutorService pool= Executors.newSingleThreadExecutor();
pool.execute(new Runnable() {
public void run() {
for(int i=0;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() {//内部类
@Override
public void run() {
if (vechicles.size()>0){//判断是否有车
boolean lighted=Lamp.valueOf(Road.this.name).isLigthed();
if(lighted){
System.out.println(vechicles.remove(0)+" "+
"is traversing");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
package com.isoftstone.interveiw.traffic;
public class MainClass {
/**
* @param args
*
* S2N,S2W,E2W,E2S,N2S,N2E,W2E,W2N,E2N,N2W,W2S,S2E
*/
public static void main(String[] args) {
//优化代码,这样的new对象简写代码
String [] directions=new String[]
{"S2N","S2W","E2W","E2S","N2S","N2E","W2E",
"W2N","E2N","N2W","W2S","S2E"};
for (int i = 0; i < directions.length; i++) {
new Road(directions[i]);
}
new LampContorller();
}
}