Knowing 15 patterns is only useful if you can tell which one a problem calls for. This page gives you a quick decision process: first pick the family (creational, structural, behavioral), then match the problem's shape to a specific pattern.
Step 1: pick the family#
Ask yourself these three questions in order:
- "Am I making new objects?" → Creational
- "Am I connecting or structuring existing things?" → Structural
- "Am I changing how things behave or communicate?" → Behavioral
The keywords in a problem statement usually give the family away:
| Family | Mental model | Keywords in the problem | Patterns |
|---|---|---|---|
| Creational | The factory | create, instantiate, new, build, construct, clone, spawn | Singleton, Factory Method, Abstract Factory, Builder, Prototype |
| Structural | The architecture | wrap, adapt, compose, bridge, proxy, decorate, simplify | Adapter, Decorator, Proxy, Composite, Facade, Bridge, Flyweight |
| Behavioral | The interaction | notify, execute, undo, traverse, switch algorithm, change behavior | Observer, Strategy, Command, State, Template Method, Mediator, Visitor |
Example sentences:
- "I need to create a database connection pool exactly once." → Creational
- "I need to wrap this old class in a new interface." → Structural
- "I need to switch between pricing algorithms." → Behavioral
Step 2: pick the pattern#
Structural: what are you doing to the structure?#
| You're... | Pattern |
|---|---|
| Making things fit together | Adapter |
| Layering features onto one object | Decorator |
| Wrapping for control (lazy loading, access checks) | Proxy |
| Building a tree of items and groups | Composite |
| Hiding a complex subsystem | Facade |
| Splitting two dimensions that vary independently | Bridge |
| Sharing data across huge numbers of objects | Flyweight |
Behavioral: what kind of behavior is varying?#
| You're... | Pattern |
|---|---|
| Switching between algorithms | Strategy |
| Notifying many objects of a change | Observer |
| Executing, queuing or undoing actions | Command |
| Changing behavior as internal state changes | State |
| Fixing a sequence of steps and varying some | Template Method |
| Passing a request along until someone handles it | Chain of Responsibility |
| Performing new operations over a fixed object structure | Visitor |
Creational: what's hard about creating it?#
| The difficulty is... | Pattern |
|---|---|
| There must be exactly one | Singleton |
| You don't know which type until runtime | Factory Method |
| You need a matching family of objects | Abstract Factory |
| Construction has many optional parts | Builder |
| Construction is expensive; copying is cheaper | Prototype |
The tricky ones#
Strategy vs Factory. Both often involve "different types", but they answer different questions.
from __future__ import annotations
# BEHAVIORAL (Strategy): switching HOW something is calculated
class PriceCalculator:
def __init__(self, strategy: PricingStrategy):
self.strategy = strategy # easy / medium / hard pricing
def calculate(self, order: Order) -> float:
return self.strategy.calculate(order)
# CREATIONAL (Factory): deciding WHICH new object to create
class EnemyFactory:
def create_enemy(self, kind: str) -> Enemy:
return Zombie() if kind == "zombie" else Vampire()
They combine well: a factory often picks which strategy to plug in.
Decorator vs Strategy. Both change what an object does, but differently.
# STRUCTURAL (Decorator): wrapping in layers that ADD behavior
coffee = Milk(Sugar(BasicCoffee()))
# structure: ((BasicCoffee wrapped in Sugar) wrapped in Milk)
# BEHAVIORAL (Strategy): REPLACING how one step is done
coffee.set_brewing(FrenchPress())
Strategy vs State. Same class shape. If the client chooses the behavior, it's Strategy; if the object switches itself as it runs, it's State.
Adapter vs Facade. Adapter makes one interface look like another; Facade puts one simple front on many classes.
Memory aid#
The first letters map straight to the family:
- Creating or instantiating → Creational
- Connecting, composing or wrapping → Structural
- Communicating, behaving or swapping algorithms → Behavioral
Key takeaways#
- Decide the family first: creating, structuring, or behaving.
- Then match the verb in the problem (fit, layer, hide, switch, notify, undo) to a pattern.
- Strategy/State, Adapter/Facade and Decorator/Strategy are the classic look-alikes; know the one-line difference for each.
- Patterns combine: a factory choosing a strategy, a command stored for undo, a composite of commands as a macro.