0
0
LLDsystem_design~7 mins

Product, Cart, Order classes in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When designing an e-commerce system, mixing responsibilities of products, shopping carts, and orders in a single class leads to code that is hard to maintain and extend. This causes bugs when updating product details or processing orders, and makes it difficult to add new features like discounts or inventory checks.
Solution
Separate the system into three classes: Product to represent item details, Cart to manage items a user wants to buy, and Order to handle the final purchase process. Each class has clear responsibilities and interacts through well-defined methods, making the system easier to understand, test, and extend.
Architecture
┌─────────┐      ┌─────────┐      ┌─────────┐
│ Product │─────▶│   Cart  │─────▶│  Order  │
└─────────┘      └─────────┘      └─────────┘
     ▲               │               │
     │               │               │
     └───────────────┴───────────────┘

This diagram shows the flow where Product instances are added to the Cart, and the Cart is then used to create an Order.

Trade-offs
✓ Pros
Clear separation of concerns improves code maintainability.
Easier to add features like discounts or inventory management in specific classes.
Simplifies testing by isolating functionality.
Improves readability and collaboration among developers.
✗ Cons
Requires more upfront design and planning.
May introduce more classes and interfaces, increasing initial complexity.
Inter-class communication needs careful design to avoid tight coupling.
Use when building any e-commerce or shopping system with multiple products and user carts, especially if the system expects growth or feature additions.
Avoid if building a very simple prototype or script with minimal features where separation adds unnecessary complexity.
Real World Examples
Amazon
Separates product catalog management, user shopping carts, and order processing to handle millions of users and complex purchase flows.
Shopify
Uses distinct classes for products, carts, and orders to allow merchants to customize each part independently.
Uber Eats
Manages menu items as products, user selections as carts, and final orders separately to support real-time updates and order tracking.
Code Example
The before code mixes product storage, cart management, and checkout in one class, making it hard to maintain. The after code separates these concerns into Product, Cart, and Order classes, each with clear responsibilities and interactions.
LLD
### Before: All logic in one class (bad design)
class ECommerce:
    def __init__(self):
        self.products = {}
        self.cart = {}

    def add_product(self, product_id, name, price):
        self.products[product_id] = {'name': name, 'price': price}

    def add_to_cart(self, product_id, quantity):
        if product_id in self.products:
            self.cart[product_id] = self.cart.get(product_id, 0) + quantity

    def checkout(self):
        total = 0
        for pid, qty in self.cart.items():
            total += self.products[pid]['price'] * qty
        self.cart.clear()
        return total


### After: Separate Product, Cart, and Order classes (good design)
class Product:
    def __init__(self, product_id: int, name: str, price: float):
        self.product_id = product_id
        self.name = name
        self.price = price

class Cart:
    def __init__(self):
        self.items = {}  # product_id -> quantity

    def add_product(self, product: Product, quantity: int):
        self.items[product.product_id] = self.items.get(product.product_id, 0) + quantity

    def get_total(self, product_catalog: dict) -> float:
        total = 0
        for pid, qty in self.items.items():
            total += product_catalog[pid].price * qty
        return total

    def clear(self):
        self.items.clear()

class Order:
    def __init__(self, cart: Cart, product_catalog: dict):
        self.cart = cart
        self.product_catalog = product_catalog
        self.total_amount = 0
        self.is_paid = False

    def place_order(self):
        self.total_amount = self.cart.get_total(self.product_catalog)
        self.is_paid = True  # Simplified
        self.cart.clear()
        return self.total_amount
OutputSuccess
Alternatives
Monolithic class
Combines product, cart, and order logic into one class without separation.
Use when: Only for very simple applications or quick prototypes with no plans for scaling.
Microservices
Splits product, cart, and order into separate services communicating over network calls.
Use when: For large-scale distributed systems requiring independent deployment and scaling.
Summary
Separating Product, Cart, and Order into distinct classes improves code clarity and maintainability.
Each class has a focused responsibility, making it easier to add features and fix bugs.
This design pattern is essential for scalable and extensible e-commerce systems.