0
0
LLDsystem_design~3 mins

Why Room type hierarchy in LLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how organizing room types like a family tree can save you hours of headaches!

The Scenario

Imagine you are managing a hotel booking system where each room has different features and prices. You try to handle each room type separately by writing separate code for single rooms, double rooms, suites, and so on.

The Problem

This manual approach quickly becomes messy and confusing. Every time you add a new room type or change a feature, you must rewrite or duplicate code. It is easy to make mistakes, and the system becomes hard to maintain and extend.

The Solution

Using a room type hierarchy lets you organize room types in a clear structure. You can define common features once and extend or customize them for specific room types. This makes your system cleaner, easier to update, and less error-prone.

Before vs After
Before
if room_type == 'single':
    price = 100
elif room_type == 'double':
    price = 150
elif room_type == 'suite':
    price = 300
After
class Room:
    def price(self):
        return 0

class SingleRoom(Room):
    def price(self):
        return 100

class DoubleRoom(Room):
    def price(self):
        return 150

class SuiteRoom(Room):
    def price(self):
        return 300
What It Enables

It enables building flexible and scalable systems where new room types can be added easily without breaking existing code.

Real Life Example

Online hotel booking platforms use room type hierarchies to manage hundreds of room variations efficiently, allowing quick updates and consistent pricing.

Key Takeaways

Manual handling of room types leads to duplicated and fragile code.

Room type hierarchy organizes shared and unique features clearly.

This approach simplifies maintenance and supports easy extension.