Skip to content

Problem to pattern quick reference

How to use this sheet

Read the left column, not the pattern names. In the room you hear a symptom ("we may add more fare rules"), and you owe one sentence: symptom, pattern, the follow-up it turns into a one-class change. The idiom column is what you would actually write in Python; the last column is a page with tested code.

Tables

Creational: something is built and the concrete class varies

Symptom you hear Pattern Python idiom Worked in
A type arrives as a string or code from outside Factory Method dict registry, classmethod constructor Parking lot
Variants must be built as a matching set, never mixed Abstract Factory frozen dataclass of callables Payment gateway
Construction takes ten arguments, most optional Builder keyword-only args, dataclasses.replace Meeting scheduler
You need another copy of an already configured object Prototype copy.deepcopy, replace Chess
"There is only one of these in the system" Singleton, usually declined build once in main and inject Parking lot

Structural: something wraps or composes

Symptom you hear Pattern Python idiom Worked in
A vendor SDK's names and error codes leak inward Adapter thin wrapper over a Protocol Payment gateway
Retry, rate limit and metrics must stack in any order Decorator @decorator, functools.wraps Notification service
Access must be checked, delayed or cached before the real call Proxy __getattr__ delegation, cached_property Library management
One user action needs six collaborators called in order Facade one module-level function Amazon order flow
Leaf and container must answer the same question Composite recursive dataclasses, __iter__ In-memory file system
Millions of objects repeat a handful of values Flyweight interning, lru_cache, Enum Chess
Two axes vary independently (kind by channel) Bridge injected implementor Notification service

Behavioural: something decides, reacts or remembers

Symptom you hear Pattern Python idiom Worked in
One operation, several interchangeable rules chosen by the caller Strategy callables, sorted(key=) Parking lot
Behaviour depends on a status the object moves itself through State Enum plus a transition table Vending machine
"Update the display whenever the score changes" Observer list of callbacks Cricinfo
Peers coordinate and the rules live nowhere Mediator one coordinator object Elevator system
Publisher and subscriber must never import each other Event Bus defaultdict(list) keyed by topic Pub/sub queue
Undo, redo, replay or an audit trail of actions Command do and undo callable pairs Text editor
A fixed sequence of steps whose middles differ Template Method ABC with hook methods Tic-tac-toe
Rules tried in order until one handles it Chain of Responsibility list of callables ATM
Ordered stages that transform and may drop the request Pipeline and Middleware reduce over callables Notification service
New operations keep arriving over a stable structure Visitor singledispatch, match In-memory file system
Callers must traverse without seeing the container Iterator generators, itertools In-memory file system
Restore an earlier state exactly, including what was deleted Memento frozen dataclass, deepcopy Key-value store
Users type filters or a small query syntax Interpreter tokenizer plus match Stack Overflow

Infrastructure edges: persistence, time and composition

Symptom you hear Pattern Python idiom Worked in
Domain logic must be tested without a database Repository Protocol plus a dict-backed fake Library management
Several writes must land together or not at all Unit of Work contextmanager around a working copy Payment gateway
Tests need a fake clock, id generator or gateway Dependency Injection constructor arguments typed as Protocol every problem here
if x is None guards are spreading through callers Null Object a do-nothing implementation of the interface Logging framework
Business rules get combined with and, or, not Specification predicates with __and__, __or__ Stack Overflow
Creating the resource costs far more than using it Object Pool queue.Queue plus a context manager Car rental

The confusion pairs, and the single question that settles each

Group Ask yourself Then say
Strategy, State, Template Method Who picks the behaviour, and when? The caller injects one of several peers that ignore each other (Strategy); the object swaps its own delegate as events arrive and each state names its successors (State); a base class fixes the skeleton and subclasses fill steps, decided when the class is written (Template Method).
Decorator, Proxy, Adapter Does the interface change, and what happens to the call? Same interface, always forwards, adds behaviour, stacks (Decorator); same interface, decides whether the real call happens for lazy loading, caching or permissions (Proxy); different interface in, no behaviour added, one-to-one over something you do not own (Adapter). Adapt first, then decorate.
Observer, Mediator, Event Bus Who knows whom? The subject holds its listeners and calls them (Observer); colleagues know only a coordinator that owns the rules (Mediator); both sides know only a topic string and dispatch may be asynchronous (Event Bus).
Facade, Adapter Simpler, or compatible? A facade invents a smaller interface over many objects so one call replaces six; an adapter supplies an existing interface over one object so old clients keep working. A facade may contain adapters; an adapter never simplifies.
Composite, Decorator How many children? Composite aggregates many children to answer as a whole; a decorator has exactly one child and exists to add behaviour.
Chain of Responsibility, Pipeline May a link refuse to forward? A chain stops at the first handler that copes; a pipeline runs every stage in order and returns a value, though a stage may short-circuit.

When the honest answer is no pattern

Tempting pattern Cheaper answer that scores the same or better
Singleton for a manager one instance created at the composition root and injected
Strategy with exactly one implementation a plain method, plus a sentence on where the seam would go
Builder for three required fields a frozen dataclass with keyword-only arguments
Observer between two objects a direct call, until a third listener exists
Abstract Factory for one family a module of functions
Visitor over two node types match on the type in one function

Memory hooks

  • "Name the axis before the pattern." If you cannot say what will change, the answer is no pattern.
  • Strategy is what, State is when, Template Method is where. Rule, lifecycle, step.
  • Decorator adds, Proxy withholds, Adapter translates. Identical boxes, three intents.
  • Observer is one-to-many, Mediator is many-to-one, Event Bus is many-to-many.
  • Command is what to do, Observer is whom to tell, Event is what happened.
  • Two implementations justify a seam; one is speculation. Say the second one out loud.

Interview tip

Say the pattern after the symptom and before the class name: "the split rule is what they will change, so it goes behind a SplitStrategy and the group only calls split; a percentage split becomes one new class and one test." That sentence is what the rubric rewards.

Common mistake

Announcing a pattern list in the first minute, then bending the design to fit it. Interviewers read that as vocabulary without judgement. Draw the Gang of Four shape when asked, then say which Python idiom you would really write, and name the pattern you deliberately declined.