Discover how organizing room types like a family tree can save you hours of headaches!
Why Room type hierarchy in LLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
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.
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.
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.
if room_type == 'single': price = 100 elif room_type == 'double': price = 150 elif room_type == 'suite': price = 300
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
It enables building flexible and scalable systems where new room types can be added easily without breaking existing code.
Online hotel booking platforms use room type hierarchies to manage hundreds of room variations efficiently, allowing quick updates and consistent pricing.
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.
Practice
Room type hierarchy in system design?Solution
Step 1: Understand the concept of hierarchy
A hierarchy groups items by common traits, making management simpler.Step 2: Apply to room types
Using a base class for shared features and subclasses for specifics avoids duplication and eases updates.Final Answer:
To organize rooms by shared and unique features for easier maintenance -> Option BQuick Check:
Hierarchy = Organize by features [OK]
- Confusing hierarchy with flat lists
- Duplicating properties in every room class
- Ignoring shared features in base class
Room with a subclass ConferenceRoom in a typical object-oriented design?Solution
Step 1: Identify correct syntax for inheritance
In many modern languages,extendsis used to inherit from a base class.Step 2: Check each option
class Room {}; class ConferenceRoom extends Room {} uses correct syntax:class ConferenceRoom extends Room {}. Others use incorrect or incomplete syntax.Final Answer:
class Room {}; class ConferenceRoom extends Room {} -> Option AQuick Check:
Inheritance syntax = extends [OK]
- Using 'inherits' instead of 'extends'
- Missing curly braces for class body
- Incorrect parentheses in class declaration
class Room:
def __init__(self, name):
self.name = name
class Bedroom(Room):
def __init__(self, name, bed_size):
super().__init__(name)
self.bed_size = bed_size
room = Bedroom('Master', 'King')
print(room.name, room.bed_size)What will be the output?
Solution
Step 1: Trace object creation
CreatingBedroom('Master', 'King')calls Bedroom's constructor, which calls Room's constructor with 'Master'.Step 2: Check printed attributes
room.nameis 'Master' from Room;room.bed_sizeis 'King' from Bedroom.Final Answer:
Master King -> Option DQuick Check:
Subclass calls base, attributes set correctly [OK]
- Assuming subclass overwrites base attributes
- Forgetting to call super().__init__
- Confusing attribute names
class Room:
def __init__(self, name):
self.name = name
class MeetingRoom(Room):
def __init__(self, name, capacity):
self.capacity = capacity
room = MeetingRoom('Boardroom', 20)
print(room.name, room.capacity)What is the issue here?
Solution
Step 1: Check constructor chaining
MeetingRoom's constructor sets capacity but does not callsuper().__init__(name), sonameis not set.Step 2: Understand effect on attributes
Without base constructor call,room.nameis missing, causing error or undefined behavior.Final Answer:
Missing call to base class constructor causes room.name to be undefined -> Option CQuick Check:
Always call base __init__ in subclass [OK]
- Assuming base constructor runs automatically
- Ignoring missing attributes in subclass
- Confusing syntax errors with logic errors
Room, Bedroom, ConferenceRoom, and Suite. Suites can have multiple bedrooms and a living area. Which design approach best models this?Solution
Step 1: Analyze relationships
Suite is a special Room that contains multiple Bedrooms and a living area, so it should inherit from Room.Step 2: Model composition
Suite should have a list of Bedroom objects (composition) to represent multiple bedrooms, plus its own living area attributes.Final Answer:
Make Suite inherit from Room and include a list of Bedroom objects plus living area -> Option AQuick Check:
Use inheritance + composition for complex types [OK]
- Using inheritance to model 'has-many' relationships
- Ignoring composition for complex room types
- Making unrelated classes inherit incorrectly
