1、大学设计模式课程实用代码大全代码淘宝店530213测试类Testpublic class Test public static void main(String args) Lift lift=new Lift(); new User(lift).gotoFloor(); lift.operate();电梯类Liftimport java.util.*;public class Lift boolean doorState=true;/true开,false 关 int currentFloor=1; ArrayList holdList=new ArrayList(); LiftState
2、state; public Lift() state=new Hold(this); public void setDoorState(boolean doorstate) this.doorState=doorstate; public void setState(LiftState state) this.state=state; public LiftState getState() return this.state; public void openDoor() setDoorState(true); System.out.println (电梯门已开,请抓紧进出!); public
3、 void closeDoor() setDoorState(false); System.out.println (电梯门已关,即将运行!); public void move(int from, int to) if(fromto) System.out.println(电梯现在+from+楼,即将+this.state.stateName+到+to+楼); for(int i=from+1;ito) System.out.println(电梯现在+from+楼,即将+this.state.stateName+到+to+楼); for(int i=from-1;ito;i-) System
4、.out.println(已到达+i+楼,不停); this.currentFloor=i-1; try Thread.sleep(1000); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); public int getNextFloor() return (Integer)(holdList.iterator().next(); public void operate() this.state.operate(); 状态类LiftStateabstract class
5、 LiftState protected Lift lift; String stateName; public LiftState(LiftState oldstate) this.lift=oldstate.lift; public LiftState() public void openDoor() lift.setDoorState(true); System.out.println (电梯门已开,请抓紧进出!); public void closeDoor() lift.setDoorState(false); System.out.println (电梯门已关,即将运行!); pu
6、blic abstract void operate(); 使用者类Userimport java.io.*;import java.util.Scanner;public class User Lift lift;public User(Lift lift) this.lift=lift;public void gotoFloor() Scanner sc = new Scanner(System.in);/获取键盘输入 int nextFloor=sc.nextInt(); lift.holdList.add(nextFloor); System.out.println(有人要去+next
7、Floor+楼);就绪类Holdimport java.util.Currency;public class Hold extends LiftState public Hold(Lift lift) this.lift=lift; this.stateName=就绪; public Hold(LiftState state) super(state); this.stateName=就绪; Override public void operate() openDoor(); / TODO Auto-generated method stub try Thread.currentThread(
8、).sleep(2000); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); closeDoor(); int nextFloor=this.lift.getNextFloor(); System.out.println(next:+nextFloor+-curent:+this.lift.currentFloor); if(nextFloorthis.lift.currentFloor) this.lift.setState(new RunUp(this); this.
9、lift.operate(); else if(nextFloorthis.lift.currentFloor) this.lift.setState(new RunDown(this); this.lift.operate(); 下降类class RunDown extends LiftState public RunDown(LiftState state) super(state); this.stateName=上升; public void openDoor() lift.setDoorState(false); System.out.println (上升状态下严禁开门!); pu
10、blic void closeDoor() lift.setDoorState(false); System.out.println (上升状态下严禁开门!); Override public void operate() / TODO Auto-generated method stub int nextFloor=(Integer)this.lift.holdList.iterator().next(); this.lift.move(this.lift.currentFloor, nextFloor); this.lift.setState(new Hold(this); System.
11、out.println(电梯现在已经到达+nextFloor+楼); this.lift.operate(); 上升类class RunUp extends LiftState public RunUp(LiftState state) super(state); this.stateName=上升; public void openDoor() lift.setDoorState(false); System.out.println (上升状态下严禁开门!); public void closeDoor() lift.setDoorState(false); System.out.print
12、ln (上升状态下严禁开门!); Override public void operate() / TODO Auto-generated method stub int nextFloor=(Integer)this.lift.holdList.iterator().next(); /System.out.println (下一个:+nextFloor); this.lift.move(this.lift.currentFloor, nextFloor); this.lift.setState(new Hold(this); System.out.println(this.lift.stat
13、e.stateName); System.out.println(电梯现在已经到达+this.lift.currentFloor+楼); this.lift.holdList.remove(0); this.lift.operate(); 故障类class Broken extends LiftState public Broken(LiftState state) super(state); this.stateName=故障; public void openDoor() lift.setDoorState(false); System.out.println (有故障!暂停使用!); p
14、ublic void closeDoor() lift.setDoorState(false); System.out.println (超重!); public void repair() this.lift.setState(new Hold(this); System.out.println (故障已排除!电梯准备就绪); Override public void operate() / TODO Auto-generated method stub System.out.println (有故障!暂停使用!); /测试类public class Test public static v
15、oid main(String args) Car car=new Car(); Walker walker=new Walker(); WalkLight wlight=new WalkLight(); RoadLight rLight=new RoadLight(); wlight.addObserver(walker); rLight.addObserver(car); rLight.addObserver(wlight); rLight.changeColor();交通灯类import java.util.*;public abstract class TrafficLight boo
16、lean currentColor=true; /true 绿色,false红色 String lighttype; ArrayList observerList=new ArrayList(); public void addObserver(TrafficObserver observer) if(!observerList.contains(observer) observerList.add(observer); public abstract void changeColor();public void hold(long time) try Thread.currentThread
17、().sleep(time); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); public void yellowHold(long time) System.out.println (lighttype+(currentColor?绿:红)+灯结束!马上变黄.); /this.road.paintLightColor(road.getGraphics(),Color.YELLOW,lighttype); System.out.println (lighttype+现在
18、是黄灯! 2秒后变+(currentColor?红:绿)+.); hold(time);/等2秒 public void lightTime(long time) System.out.println (lighttype+现在是+(currentColor?绿:红)+灯!15秒后结束.); for(int i=15;i0;i-)/倒计时15秒 System.out.println (lighttype+(currentColor?绿:红)+灯剩余+i+秒); hold(1000); public void notice() new Thread() public void run() for
19、(TrafficObserver obs:observerList) obs.response(currentColor); /Thread.currentThread().yield(); .start(); 交通灯观察者类public interface TrafficObserver public void response(boolean color);道路灯public class RoadLight extends TrafficLight public RoadLight() lighttype=机动车道; public void changeColor() / TODO Aut
20、o-generated method stub yellowHold(2000); currentColor=!currentColor; notice(); lightTime(20000); 人行道灯public class WalkLight extends TrafficLight implements TrafficObserver public WalkLight() lighttype= 人行道; Override public void changeColor() / TODO Auto-generated method stub notice(); lightTime(160
21、00); Override public void response(boolean color) / TODO Auto-generated method stub currentColor=!color; changeColor(); 汽车类public class Car implements TrafficObserver Override public void response(boolean color) / TODO Auto-generated method stub if(color) move(); else stop(); public void stop() Syst
22、em.out.println(汽车停); public void move() System.out.println(汽车行驶); 行人类public class Walker implements TrafficObserver Override public void response(boolean color) / TODO Auto-generated method stub if(color) move(); else stop(); public void stop() System.out.println(行人停); public void move() System.out.println(行人行驶);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1