軟件模式之狀態(tài)模式簡易電梯調(diào)度問題含源碼_第1頁
軟件模式之狀態(tài)模式簡易電梯調(diào)度問題含源碼_第2頁
軟件模式之狀態(tài)模式簡易電梯調(diào)度問題含源碼_第3頁
軟件模式之狀態(tài)模式簡易電梯調(diào)度問題含源碼_第4頁
軟件模式之狀態(tài)模式簡易電梯調(diào)度問題含源碼_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

軟件構(gòu)造第5次實驗實驗報告班級:學號:姓名:2016年4月20日

一.設(shè)計1.本次實驗旨在通過對電梯調(diào)度控制器系統(tǒng)的模擬來進一步理解和掌握狀態(tài)模式。首先,我們類比了糖果機的狀態(tài)圖,經(jīng)過多次的修改,完成了以上的狀態(tài)圖。如圖所示,一部電梯的初始狀態(tài)是IdleState空閑狀態(tài),當有人按下上/下樓按鈕時,若電梯位于當前樓層,則直接開/關(guān)門(OpenOrCloseState);若電梯位于其他樓層,則需要先運行(RunState)至乘客所在的樓層,當運行至乘客所在樓層,轉(zhuǎn)換至開/關(guān)門(OpenOrCloseState),然后電梯進入空閑狀態(tài)(IdleState),當乘客進入電梯后按下電梯內(nèi)的樓層數(shù)字按鈕時,電梯進入運行狀態(tài)(RunState),將乘客送至所需的樓層。

StateidleStateStateopenOrDownStateStaterunStateStatestateintourrentFloorintdestinationFloorScheduler+numberButtonWasPushed()4-upOrDownButtonWasPushed()A+setState()+setCurrentFloor))+setDestinationFloor()+open()+chse()+getldleStated+getOpenOrCtoseState()Scheduler+numberButtonWasPushed()4-upOrDownButtonWasPushed()A+setState()+setCurrentFloor))+setDestinationFloor()+open()+chse()+getldleStated+getOpenOrCtoseState()+getRunStatef)+getCurrentFIdof()+getDestinationFlDQr()+getState(J+taString()+numberButtonWasPushed()+upOrDownButtonWasPushed()-FarriveStartingl)4-arriveDestination()Schedulerscheduler4-pushUpOrDownButton()+pushNumberButton()Elevatorelevator+numberButtonWasPushed()+upOrDownButtonWasPushed()4-arriveStarting()+arriveDestination()Elevatorelevator+numberButtonWasPushed()+upOrDownButtonWasPushed()4-arriveStanting()+arriveDestination()Elevatorelevator+numberButtonWasPushed()+upOrDownButtonWasPushed()4-arriveStarting()4arriveDestination().基于狀態(tài)模式的電梯調(diào)度控制器系統(tǒng)如上圖所示。狀態(tài)模式,即允許對象在內(nèi)部狀態(tài)改變時改變它的行為,對象看起來好像是修改了它的類。在狀態(tài)模式中,客戶不會直接和狀態(tài)交互,只有Context才會對狀態(tài)提出請求。.從UML類圖中可以看到,我們?yōu)殡娞蓊?Elevator)提供了獲得各個狀態(tài)的對象方法,即getIdleState()等,這些方法是用于各個具體狀態(tài)類來實現(xiàn)狀態(tài)轉(zhuǎn)換的,如下圖所示:publicvoidjpDrDownEuttonWasPushed(intfloor)-[System.out.printIn(riYojpasbedtheUpOrDownButton.ri);if(floor=="已寸己匚口工.自已9口工工.”1口口士【))■[一若電梯位于熊客來自的樓層直接開門elevator.setState(elevator.getOpenOrClosestate());收工氏{//若電梯當前位于其他樓層,應(yīng)先運行至乘客所在的樓層elevator.setState(elevator.getRunState());elevator.setDestinationFloor(floor);setState()方法的參數(shù)應(yīng)該是對象而非類,因為可以有多部電梯,而不同電梯同一時刻的狀態(tài)可能是不同的,為了保證電梯之間的狀態(tài)不會產(chǎn)生混亂,我們需要在Elevator類中提供能夠返回每種狀態(tài)的對象的方法。二.實現(xiàn)I.State/**狀態(tài)接口@authorYilia*/publicinterfaceState{voidnumberButtonWasPushed(intfloor);//按下樓層數(shù)字按鈕(電梯內(nèi)部)參數(shù)為需要到達的樓層voidupOrDownButtonWasPushed(intfloor);//按下上/下按鈕(電梯外部)參數(shù)表示乘客來自哪一個樓層voidarriveStarting();//到達任務(wù)開始的樓層voidarriveDestination();//至九達目的的樓層)2.IdleState/**空閑狀態(tài)@authorYilia**/publicclassIdleStateimplementsState{Elevatorelevator;//intcurrentFloor;publicIdleState(Elevatorelevator){this.elevator=elevator;)publicvoidnumberButtonWasPushed(intfloor){System.out.println("YoupushedtheNumberButton.");elevator.setState(elevator.getRunState());elevator.setDestinationFloor(floor);)publicvoidupOrDownButtonWasPushed(intfloor){System.out.println("YoupushedtheUpOrDownButton.");if(floor==elevator.getCurrentF100r()){//若電梯位于乘客來自的樓層直接開門elevator.setState(elevator.getOpenOrCloseState());}else{//若電梯當前位于其他樓層,應(yīng)先運行至乘客所在的樓層elevator.setState(elevator.getRunState());elevator.setDestinationFloor(floor);}}publicvoidarriveStarting(){System.out.println("YouhavetopushtheUpOrDownButton.");}publicvoidarriveDestination(){System.out.println("Thereisnooneintheelevator.");}}3.OpenOrCloseState/***開/關(guān)門*@authorYilia**/publicclassOpenOrCloseStateimplementsState{Elevatorelevator;//intfloor;publicOpenOrCloseState(Elevatorelevator){this.elevator=elevator;}publicvoidnumberButtonWasPushed(intfloor){elevator.setState(elevator.getIdleState());}publicvoidupOrDownButtonWasPushed(intfloor){elevator.setState(elevator.getIdleState());}publicvoidarriveStarting(){elevator.setState(elevator.getIdleState());)publicvoidarriveDestination(){elevator.setState(elevator.getIdleState());))4.RunTimeState/***運行狀態(tài)*@authorYilia**/publicclassRunStateimplementsState{Elevatorelevator;intDestinationFloor;publicRunState(Elevatorelevator){this.elevator=elevator;)publicvoidnumberButtonWasPushed(intfloor){System.out.println("Theelevatorisrunningnow!");)publicvoidupOrDownButtonWasPushed(intfloor){System.out.println("Pleasewaitamunite.");)publicvoidarriveStarting(){elevator.setCurrentF100r(elevator.getDestinationF100r());System.out.println("Theelevatorarriveatthe"+elevator.getCurrentF100r()+"floor.");elevator.setState(elevator.getOpenOrCloseState());elevator.open();//人上電梯elevator.close();)publicvoidarriveDestination(){elevator.setCurrentF100r(elevator.getDestinationF100r());System.out.print1n("Theelevatorarriveatthe"+elevator.getCurrentF100r()+"floor.");elevator.setState(elevator.getOpenOrCloseState());elevator.open();//人下電梯elevator.close();)5.Elevator/**對應(yīng)GumballMachine@authorYilia**/publicclassElevator//三個狀態(tài)StateidleState;StateopenOrCloseState;StaterunState;Statestate=idleState;//State實例變量intcurrentFloor=1;//將當前樓層初始化為1樓intdestinationFloor=-1;//將目的樓層初始化為負數(shù)//構(gòu)造器:為每一個狀態(tài)創(chuàng)建狀態(tài)實例,默認電梯從1樓publicElevator(){//this.level=level;idleState=newIdleState(this);openOrCloseState=newOpenOrCloseState(this);runState=newRunState(this);state=idleState;)//構(gòu)造器:取得當前所在的樓層,為每一個狀態(tài)創(chuàng)建狀態(tài)實例publicElevator(intfloor){this.currentFloor=floor;idleState=newIdleState(this);openOrCloseState=newOpenOrCloseState(this);runState=newRunState(this);state=idleState;)//將動作委托到當前狀態(tài)//注意:ArriveStarting()和ArriveDestanation()動作方法是內(nèi)部的動作,用戶不可以直接讓電梯到達樓層publicvoidnumberButtonWasPushed(intfloor){state.numberButtonWasPushed(floor);state.arriveDestination();)publicvoidupOrDownButtonWasPushed(intfloor){state.upOrDownButtonWasPushed(floor);state.arriveStarting();)//允許狀態(tài)對象改變機器的狀態(tài)voidsetState(Statestate){this.state=state;)voidsetDestinationFloor(intfloor){this.destinationFloor=floor;)voidsetCurrentF100r(intfloor){this.currentFloor=floor;)//開關(guān)門voidopen(){System.out.println("Thedoorisopen.");)voidclose(){System.out.println("Thedoorisclose.");)// //到達指定樓層的輔助方法// voidarrive(intfloor){// System.out.println("Theelevatorisgoingtothe"+getF100r()+”floor...");7

//this.floorfloor;//this.floorfloor;// )//GetterpublicStategetIdleState(){returnidleState;)publicStategetOpenOrCloseState(){returnopenOrCloseState;)publicStategetRunState(){returnrunState;)publicintgetCurrentF100r(){returncurrentFloor;)publicintgetDestinationF100r(){returndestinationFloor;)publicStategetState(){returnstate;)//toString()publicStringtoString(){\nCurrentreturn"ELEVATOR\n"+"CurrentFloor:"+getCurrentF100r()+State:"+getState()+"\n";\nCurrent))6.Scheduler/**對應(yīng)GumballMachine@authorYilia*/publicclassScheduler{Elevatorelevator_1;Elevatorelevator_2;intfloor;publicScheduler(){elevator_1=newElevator();elevator_2=newElevator();)publicElevatorupOrDownButtonWasPushed(){if((elevator_1.getState()==elevator_1.getIdleState())&&(elevator_2.getState()==elevator_2.getIdleState())){//若都空閑,選擇離得近的intcha_1=Math.abs(elevator_1.currentFloor-floor);intcha_2=Math.abs(elevator_2.currentFloor-floor);intmin=Math.min(cha_1,cha_2);if(min==cha_1){returnelevator_1;}else{returnelevator_2;}}elseif(elevator_1.getState()==elevator_1.getIdleState()){//1號電梯空閑returnelevator_1;}elseif(elevator_2.getState()==elevator_2.getIdleState()){//2號電梯空閑returnelevator_2;}else{System.out.println("Pleasewait.Allelevatorsarebasy!");returnnull;}}publicElevatorupOrDownButtonWasPushed(intfloor){this.floor=floor;if((elevator_1.getState()==elevator_1.getIdleState())&&(elevator_2.getState()==elevator_2.getIdleState())){//若都空閑,選擇離得近的intcha_1=Math.abs(elevator_1.currentFloor-floor);intcha_2=Math.abs(elevator_2.currentFloor-floor);intmin=Math.min(cha_1,cha_2);if(min==cha1){elevator_1.upOrDownButtonWasPushed(floor);returnelevator_1;}else{elevator_2.upOrDownButtonWasPushed(floor);returnelevator_2;}}elseif(elevator_1.getState()==elevator_1.getIdleState()){//1號電梯空閑elevator_1.upOrDownButtonWasPushed(floor);returnelevator_1;}elseif(elevator_2.getState()==elevator_2.getIdleState()){//2號電梯空閑elevator_2.upOrDownButtonWasPushed(floor);returnelevator_2;}else{System.out.println("Pleasewait.Allelevatorsarebasy!");returnnull;}}publicvoidnumberButtonWasPushed(intdestinationFloor){if(upOrDownButtonWasPushed()!=null){upOrDownButtonWasPushed().numberButtonWasPushed(destinationFloor);}else{System.out.println("Pleasewait.Allelevatorsarebasy!");}}}7.PersonpublicclassPerson{Schedulerscheduler;publicPerson(Schedulerscheduler){this.scheduler=scheduler;}voidpushUpOrDownButton(intfloor){scheduler.upOrDownButtonWasPushed(floor);}voidpushNumberButton(intdestination){scheduler.numberButtonWasPushed(destination);;}}108.ClientpublicclassClient{publicstaticvoidmain(String[]aegs){Schedulerscheduler=newScheduler。;Personperson=newPerson(scheduler);person.pushUpOrDownButton(2);person.pushNumberButton(1);))運行結(jié)果:生問題叵Javadoc^聲明且控制臺滉|Client[1][Java A:\Java\jre\bin\javaw.exeYojplanedtneUpOrDownTheelevatorarriveatthe2floor.Thedoorisopen.Thedoorisclose.YojpushedtheNumberButton.Theelevatorarriveatt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論