This is a classic system design question in e-commerce platform interviews. This article demonstrates how to build a flexible and scalable business rule system through the design of a promotion rule engine. oavoservice helps you master core e-commerce technologies.
📋 Business Scenario
Design a promotion rule engine that supports:
- Threshold Discounts (e.g., $100 off on purchases over $1000)
- Percentage Discounts (e.g., 20% off)
- Buy N Get M Free (e.g., Buy 2 Get 1 Free)
- Combo Discounts (e.g., Discount when buying A + B together)
- Rule priority and mutual exclusivity
🎯 Design Goals
- Flexibility - Easy to add new rule types
- Composability - Support rule combinations
- High Performance - Quickly calculate the optimal plan
- Maintainability - Clear code structure
💡 System Design (oavoservice Solution)
Rule Abstraction
from abc import ABC, abstractmethod
from enum import Enum
class PromotionType(Enum):
DISCOUNT = "discount"
FULL_REDUCTION = "full_reduction"
BUY_N_GET_M = "buy_n_get_m"
COMBO = "combo"
class Promotion(ABC):
def __init__(self, promotion_id, name, priority=0):
self.id = promotion_id
self.name = name
self.priority = priority
self.exclusive_with = [] # Exclusive rules
@abstractmethod
def can_apply(self, cart):
pass
@abstractmethod
def calculate_discount(self, cart):
pass
@abstractmethod
def apply(self, cart):
pass
class DiscountPromotion(Promotion):
def __init__(self, promotion_id, name, discount_rate,
applicable_products=None):
super().__init__(promotion_id, name)
self.discount_rate = discount_rate
self.applicable_products = applicable_products or []
def can_apply(self, cart):
if not self.applicable_products:
return True
return any(item.product_id in self.applicable_products
for item in cart.items)
def calculate_discount(self, cart):
total = 0
for item in cart.items:
if (not self.applicable_products or
item.product_id in self.applicable_products):
total += item.price * item.quantity
return total * (1 - self.discount_rate)
def apply(self, cart):
discount = self.calculate_discount(cart)
cart.add_discount(self.name, discount)
return cart
Shopping Cart Model
class ShoppingCart:
def __init__(self):
self.items = []
self.discounts = []
self.applied_promotions = set()
def add_item(self, item):
# Implementation to add items
pass
def get_total(self):
return sum(item.get_subtotal() for item in self.items)
def add_discount(self, name, amount):
self.discounts.append({'name': name, 'amount': amount})
def get_final_price(self):
total = self.get_total()
total_discount = sum(d['amount'] for d in self.discounts)
return max(0, total - total_discount)
Promotion Engine
class PromotionEngine:
def __init__(self):
self.promotions = []
def add_promotion(self, promotion):
self.promotions.append(promotion)
self.promotions.sort(key=lambda p: p.priority, reverse=True)
def calculate_best_promotions(self, cart):
# Find the optimal combination using dynamic programming or brute force with pruning
pass
💼 How oavoservice Helps
Design Patterns - Strategy and Factory patterns Business Abstraction - Flexible rule system Performance Optimization - Optimal plan calculation Scalability - Easy to add new rules
Contact oavoservice for professional e-commerce system interview assistance!
Tags: #Ecommerce #PromotionEngine #SystemDesign #DesignPatterns #VOHelp #InterviewPrep #1point3acres
Need real interview questions? Contact WeChat Coding0201 immediately to get real questions.