0
0
LLDsystem_design~12 mins

Composition over inheritance in LLD - Architecture Diagram

Choose your learning style9 modes available
System Overview - Composition over inheritance

This system demonstrates the principle of composition over inheritance in software design. Instead of creating deep class hierarchies, it composes objects with reusable components to achieve flexible and maintainable behavior.

The key requirement is to allow objects to share behaviors by composing small, focused components rather than inheriting from large base classes.

Architecture Diagram
User
  |
  v
+-----------------+
|   Client Code   |
+-----------------+
         |
         v
+-----------------+       +-----------------+       +-----------------+
|  Vehicle Object |<----->|  Engine Module  |<----->|  Fuel Module    |
+-----------------+       +-----------------+       +-----------------+
         |
         v
+-----------------+
| Navigation Module|
+-----------------+
Components
Client Code
client
Creates and uses composed objects instead of relying on inheritance
Vehicle Object
composed_object
Main object composed of multiple behavior modules
Engine Module
component
Provides engine-related behavior to the vehicle
Fuel Module
component
Manages fuel-related behavior independently
Navigation Module
component
Adds navigation capabilities to the vehicle
Request Flow - 5 Hops
Client CodeVehicle Object
Vehicle ObjectEngine Module
Vehicle ObjectFuel Module
Vehicle ObjectNavigation Module
Vehicle ObjectClient Code
Failure Scenario
Component Fails:Engine Module
Impact:Vehicle cannot start engine but other modules like Navigation and Fuel still function
Mitigation:Replace or fix Engine Module independently without affecting other components
Architecture Quiz - 3 Questions
Test your understanding
Why does the system prefer composition over inheritance?
ATo make the system slower but more complex
BTo force all objects to share the same base class
CTo allow flexible reuse of behaviors without deep class hierarchies
DTo avoid using any reusable code
Design Principle
This design shows how composing small, focused components creates flexible and maintainable systems. It avoids rigid inheritance trees by assembling behaviors at runtime, enabling easier updates and independent failure handling.