Factory Pattern

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. ...

May 5, 2026 · 3 min · Nitin S Kulkarni
Strategy Pattern Illustration

Design Patterns Day 1: Stop Writing Endless if-else: Strategy Pattern for Real Systems

Stop Writing Endless if-else: Strategy Pattern for Real Systems You start simple. A small feature. A couple of conditions. Then it grows. And suddenly, your code looks like this: def process_payment(method, amount): if method == "credit_card": return f"Processing {amount} via Credit Card" elif method == "paypal": return f"Processing {amount} via PayPal" elif method == "upi": return f"Processing {amount} via UPI" elif method == "crypto": return f"Processing {amount} via Crypto" else: raise ValueError("Unsupported payment method") Looks fine… until it doesn’t. The Problem At first glance, this works. ...

May 4, 2026 · 3 min · Nitin S Kulkarni