0
0
LLDsystem_design~25 mins

Composition over inheritance in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Vehicle Management System
Design focuses on how to model vehicles and their features using composition instead of inheritance. Out of scope are UI design, database persistence, and network communication.
Functional Requirements
FR1: Support different types of vehicles like cars, trucks, and motorcycles
FR2: Allow vehicles to have different features such as engine types, GPS, and entertainment systems
FR3: Enable adding or removing features dynamically without changing vehicle classes
FR4: Provide a way to get vehicle details including all features
FR5: Support future extension with new vehicle types and features easily
Non-Functional Requirements
NFR1: System should be easy to maintain and extend
NFR2: Avoid deep inheritance hierarchies to reduce complexity
NFR3: Ensure code reuse without tight coupling between vehicle types and features
NFR4: Performance should be reasonable for up to 10,000 vehicles managed concurrently
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Vehicle interface or abstract class defining common vehicle behavior
Feature interfaces or classes representing individual capabilities
Composition container inside vehicle to hold features
Factory or builder to assemble vehicles with features
Client code to interact with vehicles and their features
Design Patterns
Composition design pattern
Decorator pattern for adding features dynamically
Strategy pattern for interchangeable behaviors
Interface segregation to keep features focused
Dependency injection for flexible feature assignment
Reference Architecture
  +-------------------+       +-------------------+
  |    Vehicle        |<>-----|   Feature         |
  |  (interface)      |       |  (interface)      |
  +-------------------+       +-------------------+
           ^                            ^
           |                            |
  +-------------------+       +-------------------+
  | Car               |       | EngineFeature     |
  +-------------------+       +-------------------+
  | Truck             |       | GPSFeature        |
  +-------------------+       +-------------------+
  | Motorcycle        |       | EntertainmentFeature|
  +-------------------+       +-------------------+

Vehicles hold a list of Features and delegate feature behavior to them.
Components
Vehicle Interface
Abstract class or interface in any OOP language
Defines common vehicle methods like getDetails()
Concrete Vehicle Classes
Classes like Car, Truck, Motorcycle
Implement Vehicle interface and hold features
Feature Interface
Interface for vehicle features
Defines methods for features like activate(), getDescription()
Concrete Feature Classes
Classes like EngineFeature, GPSFeature
Implement Feature interface with specific behavior
Composition Container
List or collection inside Vehicle classes
Holds features and delegates calls to them
Request Flow
1. Client creates a Vehicle object (e.g., Car)
2. Client creates Feature objects (e.g., EngineFeature, GPSFeature)
3. Client adds Feature objects to Vehicle's feature list
4. When client calls getDetails() on Vehicle, Vehicle calls getDescription() on each Feature
5. Vehicle aggregates feature descriptions and returns full details
6. Client can add or remove features at runtime without changing Vehicle class
Database Schema
Not applicable as this is a design pattern example focusing on code structure rather than persistence.
Scaling Discussion
Bottlenecks
Managing large numbers of features per vehicle can increase memory usage
Frequent dynamic feature changes may cause performance overhead
Complex feature interactions may require careful coordination
Without inheritance, some shared code might be duplicated if not factored well
Solutions
Use efficient data structures for feature storage like hash maps
Cache computed vehicle details if features rarely change
Define clear contracts and interfaces to manage feature interactions
Extract common code into utility classes or base components to avoid duplication
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying scope, 20 minutes designing the composition-based solution with diagrams, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain why inheritance can cause rigid and complex hierarchies
Show how composition allows flexible feature addition and removal
Describe how delegation works from vehicle to features
Discuss patterns like decorator and strategy that support composition
Mention trade-offs such as potential complexity in managing many small components