Design Elevator

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

We can divide into three main components:

  1. 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.
  2. 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.
  3. 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.

 

分析:

这个设计还可以,但是里面有些问题:

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. 在现实生活中也是只要的。

posted @ 2016-11-09 02:27  北叶青藤  阅读(335)  评论(0编辑  收藏  举报