githubEdit

7. Design Elevator System

Difficulty: Medium Topics: State Design Pattern, Strategy Pattern, Scheduling Algorithm (SCAN) Key Concepts: Concurrency, Request Optimization, State Management.

Phase 1: Requirements Gathering

Goals

  • Design a smart elevator system for a building (M floors, N elevators).

  • Efficiently schedule elevators to minimize wait time.

  • Handle concurrent requests safely.

1. Who are the actors?

  • Passenger: Presses buttons inside or outside the elevator.

  • System: Dispatches elevators and manages their state.

2. What are the must-have features? (Core)

  • External Request: User presses Up/Down button on a floor.

  • Internal Request: User presses a specific floor button inside the elevator.

  • Scheduling: Algorithm to decide which elevator services a request.

  • Door Control: Open/Close logic based on state.

  • Safety: Don't change direction mid-flight unless idle; respect capacity (optional).

3. What are the constraints?

  • Real-time: Decisions must be made instantly.

  • Optimization: Prefer minimizing wait time vs. minimizing power (usually wait time).


Phase 2: Use Cases

UC1: Request Elevator (External)

Actor: Passenger Flow:

  1. Passenger on Floor X presses 'UP'.

  2. System receives ExternalRequest(Floor X, UP).

  3. Dispatcher selects the best elevator (e.g., closest moving UP or Idle).

  4. Elevator adds Floor X to its stop list.

  5. Elevator arrives at Floor X, opens doors.

UC2: Select Destination (Internal)

Actor: Passenger Flow:

  1. Passenger enters elevator and presses 'Floor Y'.

  2. System receives InternalRequest(ElevatorID, Floor Y).

  3. Elevator adds Floor Y to its stop list.

  4. Elevator moves to Floor Y.


Phase 3: Class Diagram

Step 1: Core Entities

  • ElevatorSystem: Singleton controller.

  • Elevator: The physical car.

  • Request: External (Floor, Direction) or Internal (Floor).

  • Dispatcher: Implementation of scheduling logic.

  • State: Enum (IDLE, MOVING_UP, MOVING_DOWN).

UML Diagram

spinner

Phase 4: Design Patterns

1. Strategy Pattern

  • Description: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.

  • Why used: The elevator dispatching logic (DispatchStrategy) can vary (First-Come-First-Serve, Shortest Seek Time First, SCAN/Elevator Algorithm). Strategy allows hot-swapping these algorithms based on traffic patterns (e.g., Morning Rush vs. Idle time).

2. State Pattern

  • Description: Allows an object to alter its behavior when its internal state changes.

  • Why used: An Elevator has different valid actions depending on its state (e.g., cannot move() if DoorsOpen, cannot openDoors() if Moving). State pattern encapsulates this logic, preventing invalid transitions.


Phase 5: Code Key Methods

Java Implementation


Phase 6: Discussion

Algorithms

Q: Explain Scheduling Algorithms?

  • FCFS: First Come First Serve. Simple but inefficient (bad throughput).

  • SSTF: Shortest Seek Time First. Goes to nearest request. Can cause starvation for distant floors.

  • SCAN: Elevator moves all the way UP, then all the way DOWN. Efficient but suboptimal average wait.

  • LOOK: Similar to SCAN, but reverses direction as soon as there are no more requests in the current direction. Preferred.

Capacity & Safety (SDE-3 Concept)

Q: How to handle max weight realistically?

  • A: "In real life, elevators don't track the number of people, algorithms track weight. Add a WeightSensor class (Observer pattern) that publishes WeightExceededEvent. When triggered, transition to an OVERLOADED state, trigger an alarm, and disable door close mechanisms until the event resolves. Software should fail-safe."

Thread Management (SDE-3 Concept)

Q: How do you handle multiple elevators moving concurrently?

  • A: "Each Elevator should implement Runnable and run in its own thread, managed by a ScheduledExecutorService. The Dispatcher calculates the route, while the Elevator thread independently checks its StopSet and State, sleeping between floors to simulate travel time safely."

Optimization

Q: How to handle Peak Hours (Morning Rush)?

  • A: "Use Zoning. Dedicate specific elevators to serve only Ground -> Even Floors or Ground -> Odd Floors, or specific High/Low rise banks to reduce stops."


SOLID Principles Checklist

  • S (Single Responsibility): Elevator moves/stops, Controller dispatches.

  • O (Open/Closed): Can implement new DispatchStrategy (e.g., PeakHourStrategy) without changing Controller code.

  • L (Liskov Substitution): Elevator behaviors are consistent.

  • I (Interface Segregation): DispatchStrategy is focused.

  • D (Dependency Inversion): ElementSystem should depend on DispatchStrategy interface.

Last updated