
Design Patterns Day 2: Stop Hardcoding Object Creation: Factory Pattern for Real Systems
Stop Hardcoding Object Creation: Factory Pattern for Real Systems In Part 1, we used Strategy to remove if-else from business logic. Now we fix the next problem: who decides which strategy to create—and how? The Problem (Picking Up From Strategy) After introducing Strategy, you often end up here: def get_strategy(method: str) -> PaymentStrategy: if method == "credit_card": return CreditCardPayment() elif method == "paypal": return PayPalPayment() elif method == "upi": return UPIPayment() else: raise ValueError("Unsupported payment method") We removed conditionals from business logic… but moved them into selection/creation. ...
