Designing an Elevator System
Designing an Elevator System
Requirements
- The elevator system should consist of multiple elevators serving multiple floors.
- Each elevator should have a capacity limit and should not exceed it.
- Users should be able to request an elevator from any floor and select a destination floor.
- The elevator system should efficiently handle user requests and optimize the movement of elevators to minimize waiting time.
- The system should prioritize requests based on the direction of travel and the proximity of the elevators to the requested floor.
- The elevators should be able to handle multiple requests concurrently and process them in an optimal order.
- The system should ensure thread safety and prevent race conditions when multiple threads interact with the elevators.
Classes, Interfaces and Enumerations
- The Direction enum represents the possible directions of elevator movement (UP or DOWN).
- The Request class represents a user request for an elevator, containing the source floor and destination floor.
- The Elevator class represents an individual elevator in the system. It has a capacity limit and maintains a list of 4. requests. The elevator processes requests concurrently and moves between floors based on the requests.
- The ElevatorController class manages multiple elevators and handles user requests. It finds the optimal elevator to serve a request based on the proximity of the elevators to the requested floor.
- The ElevatorSystem class is the entry point of the application and demonstrates the usage of the elevator system.
Directory Structure
elevatorsystem/
├── direction.py
├── elevator.py
├── elevator_controller.py
├── elevator_system_demo.py
└── request.pyKey Design Considerations and SOLID Principles:
- Single Responsibility:
Each class is responsible for a specific aspect of the elevator system. For example, theElevatorclass manages its own requests and movement, while theElevatorControllerhandles routing user requests to the most appropriate elevator.- Open/Closed:
The system is designed to be open for extension (e.g., adding new scheduling policies) without modifying existing code.- Liskov Substitution:
The elevator components can be substituted with other implementations (for example, a different scheduling mechanism) as long as they follow the same interface/contract.- Interface Segregation:
Each class and method exposes only the functionality that is required for its responsibility (e.g., an elevator’sprocess_requests()method hides the internal details of waiting for requests).- Dependency Inversion:
The higher-level modules (such asElevatorController) depend on abstractions (e.g., the request model and public elevator methods) rather than concrete implementations.
Summary of the Design Steps and SOLID Application
-
Clarify Requirements and Identify Core Use Cases:
- The system must support multiple elevators serving multiple floors with capacity limits.
- Users can request an elevator from any floor and the system must optimize movement.
-
Identify Key Entities:
- Direction: An enumeration for elevator movement (UP/DOWN).
- Request: Represents a user request with source and destination floors.
- Elevator: Represents an individual elevator that processes its own requests.
- ElevatorController: Manages multiple elevators and routes requests efficiently.
-
Define Classes, Interfaces, and Their Attributes:
- Each class is defined in its own file with clear responsibilities and attributes.
- For example, the
Elevatorclass encapsulates current floor, direction, and pending requests.
-
Determine Core Methods Based on Use Cases:
- Public methods include
add_request(),process_requests(), andrun()inElevator. ElevatorControllerimplements methods such asrequest_elevator()andfind_optimal_elevator().
- Public methods include
-
Define Relationships Between Classes:
- Aggregation/Composition:
- The
ElevatorControlleraggregates multipleElevatorinstances. - Each
Elevatormaintains a list ofRequestobjects.
- The
- Interaction:
- The controller delegates incoming requests to the best-suited elevator based on proximity.
- Aggregation/Composition:
-
Implement Necessary Methods:
- Each class implements the required functionality (e.g., safe request handling with locks in
Elevator).
- Each class implements the required functionality (e.g., safe request handling with locks in
-
Exception Handling and Concurrency:
- The use of thread locks and condition variables in
Elevatorensures thread safety. - Elevator threads continuously process requests while new ones are added concurrently.
- The use of thread locks and condition variables in
-
Follow Good Coding Practices:
- Clear and meaningful class names, method names, and modular design ensure maintainability.
- The system is extensible and changes (such as alternative scheduling algorithms) can be made with minimal impact on other components.
-
Apply Design Patterns and SOLID Principles:
- Single Responsibility: Each class has a focused responsibility.
- Open/Closed: The system is designed for extension (for example, a new strategy in the controller) without modifying existing code.
- Liskov Substitution & Interface Segregation: Classes can be replaced or extended as long as they fulfill the expected behavior.
- Dependency Inversion: High-level modules (like
ElevatorController) depend on abstractions (e.g., the public methods ofElevator) rather than low-level details.