面向对象的分析与设计
“两块石头磨成一把石刀,石刀可以砍树,砍成木材,木材做成椅子”,
“球从一根绳子的一段移动到了另一端”,
用面向对象的方法分析设计这两种过程。
1. 用到的类:Stone、StoneSword、Tree、Wood、Chair、StoneFactory、WoodFactory。
StoneFactroy
StoneSword createSword(Stone stone1, Stone stone2);
Wood cutTree(Tree tree);
WoodFactory
createChair(Wood wood);
2. 用到的类:Ball、Rope、Point。
Ball
move();
Rope
Rope(Point start, Point end);
Point nextPoint(Point currentPoint);
设计准则:合适的方法写在合适的类里边,即哪个类里有方法要操作的属性,这个方法就写在哪个类里边。
交通灯系统的设计:
总共有12条路线,为了统一编程模型,可以假设每条路线都有一个红绿灯对其进行控制,右转弯的4条路线的控制灯可以假设称为常绿状态,另外,其他的8条线路是两两成对的,可以归为4组,所以,程序只需考虑图中标注了数字号的4条路线的控制灯的切换顺序,这4条路线相反方向的路线的控制灯跟随这4条路线切换,不必额外考虑。
Road类的编写
每个Road对象都有一个name成员变量来代表方向,有一个vehicles成员变量来代表方向上的车辆集合。
在Road对象的构造方法中启动一个线程每隔一个随机的时间向vehicles集合中增加一辆车(用一个“路线名_id”形式的字符串进行表示)。
在Road对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。
在讲Road对象的定时器代码时,因为开始阶段还没有设计Lamp类,所以,检查该方向上的灯是否为绿的代码暂时先采用短路方式。
线程编写举例:
public class Road {
private List<String> vechicles = new ArrayList<String>();
private String name =null;
public Road(final String name){ //内部类要使用外部类的局部变量,外部类的局部变量要加final
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(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);
}
}
注意点:
一、接口是不能直接new的,而在new之后添加一个大括号就是new的它的一个实现类!如:
二、内部类要使用外部类的局部变量,外部类的局部变量要加 final ;如果调用外部类的成员变量要用外部类的名字 +this+ 变量名,如: Road.this.name 。ExecutorService pool = Executors.newSingleThreadExecutor(); Pool.execute(new Runnable(){ public void run(){} });
LampController类的编写
整个系统中只能有一套交通灯控制系统,所以,LampController类最好是设计成单例。
LampController构造方法中要设定第一个为绿的灯。
LampController对象的start方法中将当前灯变绿,然后启动一个定时器,每隔10秒将当前灯变红和将下一个灯变绿。
public class LampController {
private Lamp2 currentLamp;
public LampController(){
currentLamp = Lamp2.S2N;
currentLamp.green();
/*每隔10秒将当前绿灯变为红灯,并让下一个方向的灯变绿*/
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable(){
public void run() {
System.out.println("要变灯了!快!");
currentLamp = currentLamp.red();//red()方法返回值为下一个灯 nextLamp.
}
},
10,
10,
TimeUnit.SECONDS);
}
}
定时器编程
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable(){
public void run(){
currentLamp = currentLamp.blackOut();
}
},
10,
10,
TimeUnit.SECONDS);// 运行代码,过多少时间后,每隔多少,时间单位
MainClass类的编写
用for循环创建出代表12条路线的对象。
接着再获得LampController对象并调用其start方法。
/*产生12个方向的路线*/
String [] directions = new String[]{
"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
};
for(int i=0;i<directions.length;i++){
new Road(directions[i]);
}
/*产生整个交通灯系统*/
new LampController();