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
Behavioural: something decides, reacts or remembers
Infrastructure edges: persistence, time and composition
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.