天天看點

Design Elevator

from: https://discuss.leetcode.com/topic/89/write-elevator-program-using-event-driven-programming/9

we can divide into three main components:

user

user presses the floor button (can be up or down direction) to summon the elevator.

user presses the elevator button to go to the destination floor.

button

an elevator has buttons (elevatorbutton) inside allowing user to choose the destination floor.

each floor has two buttons (floorbutton) to summon the elevator to go up or down from that floor.

when the user presses the button, it illuminates.

when the elevator reaches the desired floor, the button stops illuminating.

calls placerequest() to insert into the elevatorrequest queue when button is pressed.

elevator

open or close door.

moves up/down.

stores states such as direction (up/down), speed, currentfloor.

we need a queue to store all request, this can all be encapsulated in an elevatorrequests class. when a user presses a button, this request has to be served and is added to the processing queue. elevator requests can be scheduled using different scheduling algorithms. elevatorcontroller controls the elevator by giving it instructions such as move up/down, or start/shutdown. the elevator controller reads the next elevator request and serves it.

Design Elevator

分析:

這個設計還可以,但是裡面有些問題:

1. controller 裡還缺少process() 這樣的方法,這個方法的實作應該是一個while loop, 每次執行玩一個request, 就從elevatorrequest取下一個requet. 隻要從elevatorrequest傳回的request不為空,就應該按照request的樓層前進。而且,controller裡不但有elevator的reference, 也需要有elevatorrequest對象的reference. 這兩個reference 在controller constructor裡得到初始化。

2. button 裡需要有elevatorrequest對象的索引。

3. button裡不需要有direction. currentfloor 和 destinfloor 就決定了direction. 在現實生活中也是隻要的。