0
0
LLDsystem_design~7 mins

Restaurant, Menu, Order classes in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When designing a restaurant ordering system, mixing responsibilities in a single class leads to code that is hard to maintain and extend. For example, if the Order class also manages menu items and restaurant details, changes in one area can break unrelated parts, causing bugs and slowing development.
Solution
Separate the system into distinct classes: Restaurant to hold restaurant details, Menu to manage available dishes, and Order to handle customer orders. Each class has a clear responsibility, making the code easier to understand, test, and modify independently.
Architecture
┌─────────────┐     ┌───────────┐     ┌───────────┐
│ Restaurant  │────▶│   Menu    │────▶│   Order   │
└─────────────┘     └───────────┘     └───────────┘

This diagram shows the flow where Restaurant contains or references Menu, and Menu is used by Order to create customer orders.

Trade-offs
✓ Pros
Clear separation of concerns improves maintainability and readability.
Easier to add new features like multiple menus or order types without affecting other parts.
Simplifies testing each class independently.
✗ Cons
More classes increase initial complexity for beginners.
Requires careful design to manage interactions between classes.
May introduce slight overhead in communication between classes.
Use when building any restaurant or food ordering system that needs to scale beyond simple scripts, especially if multiple menus or order types exist.
Avoid if the system is a very simple script with only one menu and no plans for extension, where added classes add unnecessary complexity.
Real World Examples
Uber Eats
Separates restaurant info, menu items, and customer orders to allow dynamic menu updates without affecting order processing.
DoorDash
Uses distinct classes to manage restaurant details, menus, and orders enabling flexible promotions and order tracking.
Grubhub
Maintains separate models for restaurants, menus, and orders to support multiple cuisines and complex order customizations.
Code Example
The before code mixes restaurant, menu, and order logic in one class, making it hard to maintain. The after code separates concerns: Restaurant holds a Menu, Menu manages items, and Order uses Menu to validate and place orders. This separation improves clarity and extensibility.
LLD
### Before (all responsibilities mixed in one class)
class RestaurantOrder:
    def __init__(self, name):
        self.name = name
        self.menu_items = {}
        self.orders = []

    def add_menu_item(self, item_name, price):
        self.menu_items[item_name] = price

    def place_order(self, item_name):
        if item_name in self.menu_items:
            self.orders.append(item_name)
        else:
            print("Item not on menu")


### After (separated classes)
class Restaurant:
    def __init__(self, name):
        self.name = name
        self.menu = Menu()

class Menu:
    def __init__(self):
        self.items = {}

    def add_item(self, item_name, price):
        self.items[item_name] = price

    def has_item(self, item_name):
        return item_name in self.items

class Order:
    def __init__(self, menu):
        self.menu = menu
        self.ordered_items = []

    def place_order(self, item_name):
        if self.menu.has_item(item_name):
            self.ordered_items.append(item_name)
        else:
            print("Item not on menu")
OutputSuccess
Alternatives
Monolithic Class
Combines restaurant, menu, and order logic into one class without separation.
Use when: Only for very simple, one-off scripts with no future extension planned.
Database-Driven Design
Focuses on database schema design first, then generates classes, rather than designing classes first.
Use when: When database design is the primary concern and object models follow from it.
Summary
Separating Restaurant, Menu, and Order into distinct classes prevents code entanglement and eases maintenance.
Each class has a clear responsibility, improving readability and enabling independent changes.
This design supports scalability for complex restaurant ordering systems with multiple menus and order types.