Course topics

By WebNest Studio Engineering Team

Python Tutorial

Design Patterns in Python

Design patterns are proven solutions to recurring design problems, catalogued by the "Gang of Four" in 1994: creating objects flexibly, structuring relationships, and organising behaviour. Many patterns look different — often simpler — in Python, because functions are first-class objects, modules are natural singletons, and duck typing removes the need for many interfaces.

This lesson implements the most useful patterns the Pythonic way: Singleton, Factory, Builder, Adapter, Decorator, Facade, Strategy, Observer and Command, each with a runnable example.

Why it matters

Design patterns are names for solutions that developers arrive at again and again, and they form a shared vocabulary: saying "use a strategy here" is quicker than describing the code. Python's functions and modules make many of the classic patterns much shorter than in Java, and interviewers like to ask how a pattern would look in Python.

Creational Patterns

Singleton ensures one instance — in Python a module-level object or a cached function is usually enough. Factory creates objects without the caller knowing the concrete class — often a dict mapping names to classes. Builder assembles complex objects step by step, often with method chaining.

Structural Patterns

Adapter wraps an incompatible interface to match the one your code expects. Decorator adds behaviour to objects or functions without changing them — Python's @decorator syntax implements the function version directly. Facade provides one simple interface to a complex subsystem.

Behavioural Patterns

Strategy selects an algorithm at runtime — in Python, just pass a function. Observer notifies subscribers when something happens (event systems, signals). Command wraps an action as an object so it can be queued, logged or undone. Iterator is built into the language through __iter__ and generators.

Use Patterns Sparingly

Patterns are a vocabulary, not a checklist. Apply one when it removes real duplication or coupling; otherwise the simplest code wins.

Examples

Singleton, Factory and Builder

Python
from functools import cache

@cache
def get_settings():                 # Pythonic singleton: created once, then reused
    print("loading settings")
    return {"currency": "INR"}

print(get_settings() is get_settings())

class PdfExporter:
    def export(self, data): return f"PDF({len(data)} rows)"
class CsvExporter:
    def export(self, data): return f"CSV({len(data)} rows)"

EXPORTERS = {"pdf": PdfExporter, "csv": CsvExporter}

def make_exporter(kind):            # Factory
    try:
        return EXPORTERS[kind]()
    except KeyError:
        raise ValueError(f"unknown format {kind!r}") from None

print(make_exporter("csv").export([1, 2, 3]))

class QueryBuilder:                 # Builder with method chaining
    def __init__(self, table):
        self.table, self.filters, self.limit_n = table, [], None
    def where(self, condition):
        self.filters.append(condition)
        return self
    def limit(self, n):
        self.limit_n = n
        return self
    def build(self):
        sql = f"SELECT * FROM {self.table}"
        if self.filters:
            sql += " WHERE " + " AND ".join(self.filters)
        if self.limit_n:
            sql += f" LIMIT {self.limit_n}"
        return sql

print(QueryBuilder("students").where("age > 18").where("city = 'Pune'").limit(10).build())
Output
loading settings
True
CSV(3 rows)
SELECT * FROM students WHERE age > 18 AND city = 'Pune' LIMIT 10

Adapter, Decorator and Facade

Python
import functools

class LegacyPaymentApi:              # incompatible interface
    def make_payment(self, paise):
        return f"legacy paid {paise} paise"

class PaymentAdapter:                # Adapter
    def __init__(self, legacy):
        self.legacy = legacy
    def pay(self, rupees):
        return self.legacy.make_payment(int(rupees * 100))

print(PaymentAdapter(LegacyPaymentApi()).pay(49.5))

def logged(func):                    # Decorator
    @functools.wraps(func)
    def wrapper(*args):
        result = func(*args)
        print(f"{func.__name__}{args} -> {result}")
        return result
    return wrapper

@logged
def add(a, b):
    return a + b
add(2, 3)

class Inventory:
    def reserve(self, item): return f"reserved {item}"
class Billing:
    def charge(self, amount): return f"charged {amount}"
class Shipping:
    def ship(self, item): return f"shipped {item}"

class CheckoutFacade:                # Facade
    def __init__(self):
        self.inv, self.bill, self.ship = Inventory(), Billing(), Shipping()
    def place_order(self, item, amount):
        return [self.inv.reserve(item), self.bill.charge(amount), self.ship.ship(item)]

print(CheckoutFacade().place_order("book", 250))
Output
legacy paid 4950 paise
add(2, 3) -> 5
['reserved book', 'charged 250', 'shipped book']

Strategy, Observer and Command

Python
# Strategy: pass the algorithm as a function
def flat_discount(price): return price - 100
def percent_discount(price): return price * 0.9
def no_discount(price): return price

def final_price(price, strategy=no_discount):
    return strategy(price)

print([final_price(1000, s) for s in (no_discount, flat_discount, percent_discount)])

# Observer: subscribers are notified of events
class EventBus:
    def __init__(self):
        self.subscribers = {}
    def subscribe(self, event, handler):
        self.subscribers.setdefault(event, []).append(handler)
    def publish(self, event, data):
        for handler in self.subscribers.get(event, []):
            handler(data)

bus = EventBus()
bus.subscribe("order_placed", lambda o: print("email sent for", o))
bus.subscribe("order_placed", lambda o: print("stock updated for", o))
bus.publish("order_placed", "WN-101")

# Command: actions as objects that can be undone
class AddText:
    def __init__(self, doc, text):
        self.doc, self.text = doc, text
    def execute(self):
        self.doc.append(self.text)
    def undo(self):
        self.doc.pop()

doc, history = [], []
for word in ["Hello", "Python", "World"]:
    cmd = AddText(doc, word)
    cmd.execute()
    history.append(cmd)
history.pop().undo()
print(doc)
Output
[1000, 900, 900.0]
email sent for WN-101
stock updated for WN-101
['Hello', 'Python']

Common Mistakes

  • Porting Java-style patterns literally (interfaces, getInstance()) where a function or module suffices.
  • Adding patterns before there is a real need, making simple code complex.
  • Implementing Singleton with global mutable state that makes testing hard.
  • Confusing the Decorator pattern with Python decorators — related ideas, different scopes.

Exercise

Implement the strategy pattern with plain functions. checkout takes an amount and a discount function and returns the amount after the discount. Write the two discount functions.

Expected output: 900.0 then 1000

Python · starter code
# TODO: no_discount(amount) returns the amount unchanged
# TODO: ten_percent(amount) returns the amount reduced by 10%


def checkout(amount, discount):
    return discount(amount)


print(checkout(1000, ten_percent))
print(checkout(1000, no_discount))
Show hints
  • A function can be passed as an argument without parentheses: checkout(1000, ten_percent).
  • Reducing by 10% is multiplying by 0.9.
Show solution
Python · solution
def no_discount(amount):
    return amount


def ten_percent(amount):
    return amount * 0.9


def checkout(amount, discount):
    return discount(amount)


print(checkout(1000, ten_percent))
print(checkout(1000, no_discount))

Quick quiz

1. Which pattern allows an algorithm to be chosen or swapped at runtime?

  • A. Singleton
  • B. Facade
  • C. Adapter
  • D. Strategy
Show answer

D. The behaviour is passed in, as a function or an object.

2. What does the singleton pattern guarantee?

  • A. That a class has only one instance
  • B. That a class cannot be inherited from
  • C. That objects are immutable
  • D. That methods run only once
Show answer

A. It is used for a shared resource such as configuration.

3. In the observer pattern, what happens when the subject changes?

  • A. Every registered observer is notified
  • B. It is deleted
  • C. It copies itself
  • D. Nothing
Show answer

A. Event systems and signals are built on this pattern.

Interview questions

How would you implement a singleton in Python?

Show answer

The simplest way is a module: a module is imported once and every importer receives the same object, so a value created at module level is already a singleton. Where a class is required, __new__ can be overridden to return one stored instance, or a decorator or metaclass can do the same. The module approach is preferred because it is the simplest and easiest to test.

How is the decorator pattern related to Python decorators?

Show answer

The decorator pattern wraps an object in another object with the same interface in order to add behaviour without changing the original class. A Python decorator, written with @, wraps a function or class in another function to add behaviour such as logging, caching or access checks. They share the idea of wrapping to extend, but a Python decorator is a language feature applied when the function is defined.

Key Points to Remember

  • Creational: Singleton (module/cached function), Factory (dict of classes), Builder (chaining).
  • Structural: Adapter, Decorator (@ syntax), Facade.
  • Behavioural: Strategy (pass functions), Observer (event bus), Command (undoable actions).
  • Python's first-class functions and duck typing simplify many patterns.
  • Use patterns to solve real problems, not by default.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering