Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Combining multiple design patterns in one system
Client
OrderManagementFacade
Facade
OrderBuilder
Builder
ProductFactory
Factory
PaymentStrategy
Strategy
NotificationSystem
Observer
CommandInvoker
Command
OrderConfirmation
Creational Need
class ProductFactory:
@staticmethod
def create_product(product_type, **kwargs):
if product_type == "physical":
return PhysicalProduct(**kwargs)
elif product_type == "digital":
return DigitalProduct(**kwargs)
elif product_type == "subscription":
return SubscriptionProduct(**kwargs)product_typeCreational Need
class OrderBuilder:
def __init__(self):
self.order = Order()
def add_product(self, product):
self.order.products.append(product)
return self
def set_customer(self, customer):
self.order.customer = customer
return self
def build(self):
return self.orderreturn selfStructural Need
class OrderProcessorDecorator:
def __init__(self, processor):
self.processor = processor
def process(self, order):
return self.processor.process(order)
class TaxCalculatorDecorator(OrderProcessorDecorator):
def process(self, order):
result = self.processor.process(order)
# add tax calculation
return resultprocess methodStructural Need
class OrderManagementFacade:
def __init__(self):
self.inventory = InventoryService()
self.payment = PaymentService()
self.shipping = ShippingService()
def place_order(self, order):
self.inventory.reserve_items(order.products)
result = self.payment.process_payment(order)
if result.success:
self.shipping.schedule_delivery(order)
return resultinventory, payment, and shippingBehavioral Need
class PaymentProcessor:
def __init__(self):
self.strategy = None
def set_payment_strategy(self, strategy):
self.strategy = strategy
def process_payment(self, order):
return self.strategy.pay(order.total_amount)processorBehavioral Need
class Order(Subject):
def __init__(self):
super().__init__()
self._status = "pending"
@status.setter
def status(self, new_status):
self._status = new_status
self.notify(f"Order status changed to {new_status}")OrderBehavioral Need
class PlaceOrderCommand(Command):
def __init__(self, order_service, order):
self.order_service = order_service
self.order = order
def execute(self):
return self.order_service.place_order(self.order)
def undo(self):
return self.order_service.cancel_order(self.order)execute and undoObject Creation
Direct Constructor
Factory Pattern
Builder Pattern
Singleton Pattern
Interface Compatibility
Adapter Pattern
Decorator Pattern
Facade Pattern
Communication
Observer Pattern
Strategy Pattern
Command Pattern
| Pattern | Problem Solved | Why Chosen | Alternative Considered |
|---|---|---|---|
| Factory | Different product types | Runtime type determination | Direct instantiation (too rigid) |
| Builder | Complex order creation | Many optional parameters | Constructor overloading (unwieldy) |
| Strategy | Payment methods | Runtime algorithm switching | If-else chains (hard to extend) |
| Observer | Notifications | Decoupled communication | Direct method calls (tight coupling) |
| Decorator | Order processing | Flexible responsibility chain | Inheritance (explosion of classes) |
| Command | Order operations | Undo/redo + queuing | Direct method calls (no undo) |
| Facade | System complexity | Simplified interface | Direct service calls (complex client) |
Factory, Builder) tame object creation complexityDecorator, Facade) manage composition and simplify APIsStrategy, Observer, Command) manage communication and operationsMahmoud Abas|Real-World Case Study: E-Commerce Order Processing System