System design · LLD design

Elevator System

**

FR

1.N floors and m elevators , with a max occupancy count of 8 people
2.two types of call : Hall Call -> provides a direction as input can be up or down
CabinCall -> provides a targeted floor on which it wants to go
3. On a hall call - assign elevator base on priority logic , nearest elevator moving in same direction and if nothing then idle elevator
4. On a emergeny call - emergency mode - elevator can be sent for priority use, and ignore the current queue
5.If any elevator out-of-serivce don’t assign any request to this one
6. On a cabin call - add the request for the specific elevator and when reached remove the request
7. If no Available elevator add this to a queue and assign to elevator when free

Non FR
1.Extensible: to add swappable logic to assign elevator
2.Scalable: to handle addition of elevators and floors in current system
3.Concurrency: handle concurrent requests, both across different elevators (avoid duplicate assignment) and within a single elevator
4. No starvation : request that is technically valid and pending, but never gets serviced because the system’s decision rule keeps favoring other requests over it.

Entities and classes

RequestSource: ENUM
External , Internal , Emergency

Direction: UP, DOWN , IDLE

Request:
+direction : Direction
+targetFloor: int
+source TargetSource

Eg: ExternalCall Request (UP, 5, EXTERNAL) - in this user is at 5th floor and requested for UP direct
EmergencyCall Request (UP, 5, EMERGENCY) - in this user is at 5th floor and requested for UP direct

InternalCall Request (IDLE, 5, INTERNAL) - this mean user is inside the cabin and wants to go on 5th floor

Runnable interface
+run()

Elevator: implement runnable
-elevatorID : Int
-capacity : int
-currentFloor : AtomicInteger
-state: ElevatorState
-isRunning : boolean
-upRequests TreeSet<Integer>
-downRequest TreeSet<Integer>
+addRequest(request)
+move()
+run() -checks isRunning then call move()

ElevatorState: interface
+move()
+addRequest(elevator, request)
: increase the capacity
+getDirection()
+forceRequest(elevator, request)
: handle request with emergency in this to remove up and down request for this elevator

IdleState: implements Elevatorstate
+move()
+addRequest()
+forceRequest(elevator, request)
+getDirection()

MovingUpState: implements Elevatorstate
+move()
+addRequest()
+forceRequest(elevator, request)
+getDirection()

MovingDownState: implements Elevatorstate
+move()
+addRequest()
+forceRequest(elevator, request)
+getDirection()

SelectionType - ENUM
-NORMAL, EMERGENCY

ElevatorSelectionStrategy - interface
+selectElevator(elevators, request)

NearestElevatorStrategy - implements ElevatorSelectionStrategy

EmergencyElevatorStrategy- implements ElevatorSelectionStrategy
- in this assign any elevator

ElevatorSelectionFactory
- elevatorSelectionStrategyRequest - based on SelectionType initialize selection strategy

ElevatorSystem - SINGELTON
-instance ElevatoSystem
-elevators Map<Integer, Elevator>
-selectionStrategy ElevatorSelectionFactory : Map<Enum, ElevatorSelectionStrategy>

+getInstance(numElevtors) : ElevatorSystem

+requestElevator(floor, direct) - for external call
Create a request - for external call
Select elevator using selectionStrategy.get(NORMAL)
If elevator present add in addrequest() for elevator

+selectFloor(elevatorId, floor)
Create a request - for internal call
If elevator present add in addrequest() for elevator

+emergencyRequestElevator(foor, direction)
Create a request - for external call
Select elevator using selectionStrategy.get(EMERGENCY)
If elevator present add in forceRequest()