Skip to content

Instantly share code, notes, and snippets.

@kuntalchandra
Created April 1, 2026 11:40
Show Gist options
  • Select an option

  • Save kuntalchandra/2ea0a6e1d83fec923c9882cc0729d5bd to your computer and use it in GitHub Desktop.

Select an option

Save kuntalchandra/2ea0a6e1d83fec923c9882cc0729d5bd to your computer and use it in GitHub Desktop.
LLD of generic e-commerce
"""
Core Design Principles Used- Strategy Pattern
Payment methods
Notification channels
Runtime polymorphism
Loose coupling
Extensible services
User → Cart → Order → Payment → Notification
"""
from enum import Enum
import uuid
# Constants
class OrderStatus(Enum):
CREATED = "CREATED"
PAID = "PAID"
FAILED = "FAILED"
class Product:
def __init__(self, product_id, name, price):
self.product_id = product_id
self.name = name
self.price = price
class User:
def __init__(self, user_id, name, email):
self.user_id = user_id
self.name = name
self.email = email
class Cart:
def __init__(self, user: User):
self.user = user
self.items = {}
def add_item(self, product: Product, quantity=1):
if product.product_id in self.items:
self.items[product.product_id]["quantity"] += quantity
else:
self.items[product.product_id] = {
"product": product,
"quantity": quantity
}
def get_total(self):
return sum(
item["product"].price * item["quantity"]
for item in self.items.values()
)
class Order:
def __init__(self, user: User, cart: Cart):
self.order_id = str(uuid.uuid4())
self.user = user
self.items = cart.items
self.total_amount = cart.get_total()
self.status = OrderStatus.CREATED
class PaymentStrategy:
def pay(self, amount):
raise NotImplementedError
class UpiPayment(PaymentStrategy):
def pay(self, amount):
print(f"Paid {amount} using UPI")
return True
class NetBankingPayment(PaymentStrategy):
def pay(self, amount):
print(f"Paid {amount} using Net Banking")
return True
class PaymentService:
def __init__(self, strategy: PaymentStrategy):
self.strategy = strategy
def process_payment(self, amount):
return self.strategy.pay(amount)
class NotificationStrategy:
def send(self, user: User, message):
raise NotImplementedError
# Concrete notification channels
class EmailNotification(NotificationStrategy):
def send(self, user, message):
print(f"Email sent to {user.email}: {message}")
class SMSNotification(NotificationStrategy):
def send(self, user, message):
print(f"SMS sent to {user.name}: {message}")
class NotificationService:
def __init__(self, strategy: NotificationStrategy):
self.strategy = strategy
def notify(self, user, message):
self.strategy.send(user, message)
# Order service - Orchestration layer
class OrderService:
def __init__(self):
self.orders = {}
def place_order(self, user, cart, payment_strategy, notification_strategy):
order = Order(user, cart)
payment_service = PaymentService(payment_strategy)
success = payment_service.process_payment(order.total_amount)
if success:
order.status = OrderStatus.PAID
else:
order.status = OrderStatus.FAILED
self.orders[order.order_id] = order
notification_service = NotificationService(notification_strategy)
notification_service.notify(user, f"Order {order.status.value}")
return order
# Example usage
if __name__ == "__main__":
user = User("u1", "Kuntal", "kuntal@mail.com")
p1 = Product("p1", "Laptop", 50000)
p2 = Product("p2", "Mouse", 500)
cart = Cart(user)
cart.add_item(p1, 1)
cart.add_item(p2, 2)
order_service = OrderService()
order = order_service.place_order(
user,
cart,
CreditCardPayment(), # Strategy
EmailNotification() # Strategy
)
print("Order Status:", order.status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment