Part 3 of 6 · 1 min read
Classes
A car, a dispatcher and a building.
| Class | Job in one sentence |
|---|---|
Dir |
enum: UP, DOWN, IDLE |
Door |
enum: OPEN, CLOSED |
Elevator |
One car: floor, direction, door and a sorted set of stops |
Dispatcher |
Interface that picks a car for a hall call (strategy) |
NearestDispatcher |
Picks the car with the lowest cost |
Building |
Holds the cars and the dispatcher, takes calls and presses, and advances time |
The stops are a TreeSet<Integer>. A sorted set gives the highest and lowest stop at once, and the next stop above or below any floor. It also ignores a second press of the same floor, which is the right behaviour for a button.
Time is a method, tick(), and not a thread. A test calls it as many times as needed. A real system would call it from a timer. This is the same trick as the injected clock of Level 5, module 5.
Check yourself: 1. Why a sorted set for stops? 2. Why is time a method and not a thread? 3. Which class is a strategy?