Build a clean, extensible, and object-oriented Elevator System using SOLID principles and Java design patterns.
π§ Table of Contents
- Introduction
- System Requirements
- Design Goals
- High-Level Design
- Key Classes & Responsibilities
- Design Patterns Used
- UML Diagram
- Code Implementation
- Sample Output
- 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 |
+---------------------+
π§± Code Implementation
β
Direction.java
public enum Direction {
UP, DOWN, IDLE
}
β
DoorState.java
public enum DoorState {
OPEN, CLOSED
}
β
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");
}
}
β
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();
}
}
β
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) {}
}
}
}
π¨οΈ 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
β 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)