大学设计模式课程实用代码大全代码.docx

上传人:b****3 文档编号:5429458 上传时间:2022-12-16 格式:DOCX 页数:13 大小:17.02KB
下载 相关 举报
大学设计模式课程实用代码大全代码.docx_第1页
第1页 / 共13页
大学设计模式课程实用代码大全代码.docx_第2页
第2页 / 共13页
大学设计模式课程实用代码大全代码.docx_第3页
第3页 / 共13页
大学设计模式课程实用代码大全代码.docx_第4页
第4页 / 共13页
大学设计模式课程实用代码大全代码.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

大学设计模式课程实用代码大全代码.docx

《大学设计模式课程实用代码大全代码.docx》由会员分享,可在线阅读,更多相关《大学设计模式课程实用代码大全代码.docx(13页珍藏版)》请在冰豆网上搜索。

大学设计模式课程实用代码大全代码.docx

大学设计模式课程实用代码大全代码

淘宝店530213

测试类Test

publicclassTest{

publicstaticvoidmain(String[]args){

Liftlift=newLift();

newUser(lift).gotoFloor();

lift.operate();

}

}

电梯类Lift

importjava.util.*;

publicclassLift{

booleandoorState=true;//true开,false关

intcurrentFloor=1;

ArrayListholdList=newArrayList();

LiftStatestate;

publicLift(){

state=newHold(this);

}

publicvoidsetDoorState(booleandoorstate){

this.doorState=doorstate;

}

publicvoidsetState(LiftStatestate){

this.state=state;

}

publicLiftStategetState(){

returnthis.state;

}

publicvoidopenDoor(){

setDoorState(true);

System.out.println("电梯门已开,请抓紧进出!

");

}

publicvoidcloseDoor(){

setDoorState(false);

System.out.println("电梯门已关,即将运行!

");

}

publicvoidmove(intfrom,intto){

if(from

System.out.println("电梯现在"+from+"楼,即将"+this.state.stateName+"到"+to+"楼");

for(inti=from+1;i

System.out.println("已到达"+i+"楼,不停");

this.currentFloor=i+1;

try{

Thread.sleep(1000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

elseif(from>to){

System.out.println("电梯现在"+from+"楼,即将"+this.state.stateName+"到"+to+"楼");

for(inti=from-1;i>to;i--){

System.out.println("已到达"+i+"楼,不停");

this.currentFloor=i-1;

try{

Thread.sleep(1000);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

}

publicintgetNextFloor(){

return(Integer)(holdList.iterator().next());

}

publicvoidoperate(){

this.state.operate();

}

}

状态类LiftState

abstractclassLiftState{

protectedLiftlift;

StringstateName;

publicLiftState(LiftStateoldstate){

this.lift=oldstate.lift;

}

publicLiftState(){}

publicvoidopenDoor(){

lift.setDoorState(true);

System.out.println("电梯门已开,请抓紧进出!

");

}

publicvoidcloseDoor(){

lift.setDoorState(false);

System.out.println("电梯门已关,即将运行!

");

}

publicabstractvoidoperate();

}

使用者类User

importjava.io.*;

importjava.util.Scanner;

 

publicclassUser{

Liftlift;

publicUser(Liftlift){

this.lift=lift;

}

publicvoidgotoFloor(){

Scannersc=newScanner(System.in);//获取键盘输入

intnextFloor=sc.nextInt();

lift.holdList.add(nextFloor);

System.out.println("有人要去"+nextFloor+"楼");

}

}

就绪类Hold

importjava.util.Currency;

publicclassHoldextendsLiftState{

publicHold(Liftlift){

this.lift=lift;

this.stateName="就绪";

}

publicHold(LiftStatestate){

super(state);

this.stateName="就绪";

}

@Override

publicvoidoperate(){

openDoor();

//TODOAuto-generatedmethodstub

try{

Thread.currentThread().sleep(2000);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

 

closeDoor();

intnextFloor=this.lift.getNextFloor();

System.out.println("next:

"+nextFloor+"----curent:

"+this.lift.currentFloor);

if(nextFloor>this.lift.currentFloor){

this.lift.setState(newRunUp(this));

this.lift.operate();

}

elseif(nextFloor

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("行人行驶");

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1