DEV Community

DevCorner2
DevCorner2

Posted on

πŸ›— Designing an Elevator System in Java – LLD with OOP and Design Patterns

Build a clean, extensible, and object-oriented Elevator System using SOLID principles and Java design patterns.


🧭 Table of Contents

  1. Introduction
  2. System Requirements
  3. Design Goals
  4. High-Level Design
  5. Key Classes & Responsibilities
  6. Design Patterns Used
  7. UML Diagram
  8. Code Implementation
  9. Sample Output
  10. Conclusion & Next Steps

πŸ”° Introduction

Elevator Systems are classic LLD questions in interviews. They test your ability to:

  • Model real-world entities
  • Handle state transitions
  • Implement event-driven logic

This blog presents a Java-based elevator simulation with key OOP concepts like encapsulation, abstraction, and behavioral modeling.


βœ… System Requirements

🎯 Functional Requirements

  • Elevator moves up/down between floors.
  • Accepts floor requests from users (inside/outside).
  • Stops at the correct floor and opens/closes doors.
  • Handles multiple requests in order.

βš™οΈ Non-Functional Requirements

  • Modular design
  • Easily extendable for multi-elevator logic
  • Simulated time delay (optional)

🎯 Design Goals

  • Use OOPs to model elevator state and movement.
  • Ensure separation of concerns: controller vs. elevator vs. request.
  • Implement state machine behavior (idle, moving, door open, etc.).
  • Keep code testable and extensible.

πŸ—οΈ High-Level Design

🧱 Core Components

Component Responsibility
Elevator Represents one elevator and its state
ElevatorController Manages requests and delegates actions
Request Encapsulates floor requests
Direction Enum for elevator direction (UP/DOWN/IDLE)
DoorState Enum for OPEN/CLOSED state

🧰 Design Patterns Used

Pattern Purpose
State Represent and handle elevator state transitions
Command Encapsulate floor requests as command objects
Strategy (optional) For different scheduling algorithms

πŸ“ UML Diagram (Simplified)

+---------------------+
|     Elevator        |
+---------------------+
| - id: int           |
| - currentFloor: int |
| - direction: Direction |
| - doorState: DoorState |
| - targetFloors: Queue<Integer> |
+---------------------+
| +move()             |
| +addRequest(int)    |
+---------------------+

+---------------------------+
|    ElevatorController     |
+---------------------------+
| - elevator: Elevator      |
| +submitRequest(int)       |
| +step()                   |
+---------------------------+

+---------------------+
|    Request          |
+---------------------+
| - floor: int        |
| +getFloor(): int    |
+---------------------+

+---------------------+
|    Direction (Enum) |
|  UP, DOWN, IDLE     |
+---------------------+

+---------------------+
|    DoorState (Enum) |
|  OPEN, CLOSED       |
+---------------------+
Enter fullscreen mode Exit fullscreen mode

🧱 Code Implementation

βœ… Direction.java

public enum Direction {
    UP, DOWN, IDLE
}
Enter fullscreen mode Exit fullscreen mode

βœ… DoorState.java

public enum DoorState {
    OPEN, CLOSED
}
Enter fullscreen mode Exit fullscreen mode

βœ… Elevator.java

public class Elevator {
    private int id;
    private int currentFloor = 0;
    private Direction direction = Direction.IDLE;
    private DoorState doorState = DoorState.CLOSED;
    private Queue<Integer> targetFloors = new LinkedList<>();

    public Elevator(int id) {
        this.id = id;
    }

    public void addRequest(int floor) {
        targetFloors.offer(floor);
    }

    public void move() {
        if (targetFloors.isEmpty()) {
            direction = Direction.IDLE;
            System.out.println("Elevator " + id + " is idle.");
            return;
        }

        int target = targetFloors.peek();
        if (currentFloor == target) {
            openDoor();
            targetFloors.poll(); // Remove served floor
            closeDoor();
        } else if (currentFloor < target) {
            direction = Direction.UP;
            currentFloor++;
        } else {
            direction = Direction.DOWN;
            currentFloor--;
        }

        System.out.println("Elevator " + id + " at floor " + currentFloor + " going " + direction);
    }

    private void openDoor() {
        doorState = DoorState.OPEN;
        System.out.println("Door OPEN at floor " + currentFloor);
    }

    private void closeDoor() {
        doorState = DoorState.CLOSED;
        System.out.println("Door CLOSED");
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… ElevatorController.java

public class ElevatorController {
    private Elevator elevator;

    public ElevatorController(Elevator elevator) {
        this.elevator = elevator;
    }

    public void submitRequest(int floor) {
        elevator.addRequest(floor);
    }

    public void step() {
        elevator.move();
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Main.java

public class Main {
    public static void main(String[] args) {
        Elevator elevator = new Elevator(1);
        ElevatorController controller = new ElevatorController(elevator);

        controller.submitRequest(3);
        controller.submitRequest(1);
        controller.submitRequest(5);

        for (int i = 0; i < 10; i++) {
            controller.step();
            try { Thread.sleep(1000); } catch (InterruptedException ignored) {}
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ–¨οΈ Sample Output

Elevator 1 at floor 1 going UP
Elevator 1 at floor 2 going UP
Elevator 1 at floor 3 going UP
Door OPEN at floor 3
Door CLOSED
Elevator 1 at floor 2 going DOWN
Elevator 1 at floor 1 going DOWN
Door OPEN at floor 1
Door CLOSED
Elevator 1 at floor 2 going UP
Elevator 1 at floor 3 going UP
Elevator 1 at floor 4 going UP
Elevator 1 at floor 5 going UP
Door OPEN at floor 5
Door CLOSED
Enter fullscreen mode Exit fullscreen mode

βœ… Conclusion & Next Steps

This implementation covers the core logic of a single elevator system using clean OOP design. It supports:

  • Directional movement
  • Door operations
  • Sequential request handling

πŸš€ Possible Enhancements

  • Support multiple elevators
  • Add request prioritization (based on direction)
  • Integrate a scheduler strategy
  • Add emergency halt and weight limit checks
  • Integrate with GUI or WebSocket for real-time visualization

Top comments (0)