this.lift.setState(newRunDown(this));
this.lift.operate();
}
}
}
下降类
classRunDownextendsLiftState{
publicRunDown(LiftStatestate){
super(state);
this.stateName="上升";
}
publicvoidopenDoor(){
lift.setDoorState(false);
System.out.println("上升状态下严禁开门!
");
}
publicvoidcloseDoor(){
lift.setDoorState(false);
System.out.println("上升状态下严禁开门!
!
");
}
@Override
publicvoidoperate(){
//TODOAuto-generatedmethodstub
intnextFloor=(Integer)this.lift.holdList.iterator().next();
this.lift.move(this.lift.currentFloor,nextFloor);
this.lift.setState(newHold(this));
System.out.println("电梯现在已经到达"+nextFloor+"楼");
this.lift.operate();
}
}
上升类
classRunUpextendsLiftState{
publicRunUp(LiftStatestate){
super(state);
this.stateName="上升";
}
publicvoidopenDoor(){
lift.setDoorState(false);
System.out.println("上升状态下严禁开门!
");
}
publicvoidcloseDoor(){
lift.setDoorState(false);
System.out.println("上升状态下严禁开门!
!
");
}
@Override
publicvoidoperate(){
//TODOAuto-generatedmethodstub
intnextFloor=(Integer)this.lift.holdList.iterator().next();
//System.out.println("下一个:
"+nextFloor);
this.lift.move(this.lift.currentFloor,nextFloor);
this.lift.setState(newHold(this));
System.out.println(this.lift.state.stateName);
System.out.println("电梯现在已经到达"+this.lift.currentFloor+"楼");
this.lift.holdList.remove(0);
this.lift.operate();
}
}
故障类
classBrokenextendsLiftState{
publicBroken(LiftStatestate){
super(state);
this.stateName="故障";
}
publicvoidopenDoor(){
lift.setDoorState(false);
System.out.println("有故障!
暂停使用!
");
}
publicvoidcloseDoor(){
lift.setDoorState(false);
System.out.println("超重!
!
");
}
publicvoidrepair(){
this.lift.setState(newHold(this));
System.out.println("故障已排除!
电梯准备就绪");
}
@Override
publicvoidoperate(){
//TODOAuto-generatedmethodstub
System.out.println("有故障!
暂停使用!
");
}
}
//////////////////////////////////////////////////////////////
测试类
publicclassTest{
publicstaticvoidmain(String[]args){
Carcar=newCar();
Walkerwalker=newWalker();
WalkLightwlight=newWalkLight();
RoadLightrLight=newRoadLight();
wlight.addObserver(walker);
rLight.addObserver(car);
rLight.addObserver(wlight);
rLight.changeColor();
}
}
交通灯类
importjava.util.*;
publicabstractclassTrafficLight{
booleancurrentColor=true;//true绿色,false红色
Stringlighttype;
ArrayListobserverList=newArrayList();
publicvoidaddObserver(TrafficObserverobserver){
if(!
observerList.contains(observer))
observerList.add(observer);
}
publicabstractvoidchangeColor();
publicvoidhold(longtime){
try{
Thread.currentThread().sleep(time);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicvoidyellowHold(longtime){
System.out.println(lighttype+(currentColor?
"绿":
"红")+"灯结束!
马上变黄...");
//this.road.paintLightColor(road.getGraphics(),Color.YELLOW,lighttype);
System.out.println(lighttype+"现在是黄灯!
2秒后变"+(currentColor?
"红":
"绿")+"...");
hold(time);//等2秒
}
publicvoidlightTime(longtime){
System.out.println(lighttype+"现在是"+(currentColor?
"绿":
"红")+"灯!
15秒后结束...");
for(inti=15;i>0;i--){//倒计时15秒
System.out.println(lighttype+(currentColor?
"绿":
"红")+"灯剩余"+i+"秒");
hold(1000);
}
}
publicvoidnotice(){
newThread(){
publicvoidrun(){
for(TrafficObserverobs:
observerList){
obs.response(currentColor);
//Thread.currentThread().yield();
}
}
}.start();
}
}
交通灯观察者类
publicinterfaceTrafficObserver{
publicvoidresponse(booleancolor);
}
道路灯
publicclassRoadLightextendsTrafficLight{
publicRoadLight(){
lighttype="机动车道";
}
publicvoidchangeColor(){
//TODOAuto-generatedmethodstub
yellowHold(2000);
currentColor=!
currentColor;
notice();
lightTime(20000);
}
}
人行道灯
publicclassWalkLightextendsTrafficLightimplementsTrafficObserver{
publicWalkLight(){
lighttype="人行道";
}
@Override
publicvoidchangeColor(){
//TODOAuto-generatedmethodstub
notice();
lightTime(16000);
}
@Override
publicvoidresponse(booleancolor){
//TODOAuto-generatedmethodstub
currentColor=!
color;
changeColor();
}
}
汽车类
publicclassCarimplementsTrafficObserver{
@Override
publicvoidresponse(booleancolor){
//TODOAuto-generatedmethodstub
if(color)
move();
else
stop();
}
publicvoidstop(){
System.out.println("汽车停");
}
publicvoidmove(){
System.out.println("汽车行驶");
}
}
行人类
publicclassWalkerimplementsTrafficObserver{
@Override
publicvoidresponse(booleancolor){
//TODOAuto-generatedmethodstub
if(color)
move();
else
stop();
}
publicvoidstop(){
System.out.println("行人停");
}
publicvoidmove(){
System.out.println("行人行驶");
}
}