0 XP · 0 scenes

Design Patterns Cheat Sheet: When to Use Each of the 23 Patterns

By Vivek Kumar Bhardwaj · Updated · 5 min read

A design pattern is a named solution to a problem that keeps showing up in object-oriented code. Knowing the names helps you talk about a design quickly, and knowing the problem each one solves helps you pick the right one. Do not use a pattern because it exists. Use it when you feel the pain it removes.

Each link below opens a lesson with the problem, a before and after class diagram, a step-by-step flow of the calls and code in Python and Java.

SOLID principles

Five habits that keep classes easy to change. Patterns are just these habits with names.

PatternWhat it does
Single ResponsibilityA class should have one reason to change
Open / ClosedOpen for extension, closed for modification
Liskov SubstitutionA subclass must work wherever its parent is expected
Interface SegregationMany small interfaces beat one fat one
Dependency InversionDepend on abstractions, not on concrete classes

Creational patterns

How objects get made without the caller knowing the messy details.

PatternWhat it does
SingletonExactly one instance, reachable from anywhere
Factory MethodLet a method decide which class to create
Abstract FactoryCreate whole families of matching objects
BuilderBuild a complex object one step at a time
PrototypeMake new objects by copying an existing one

Structural patterns

How classes and objects are put together into bigger, flexible shapes.

PatternWhat it does
AdapterMake an incompatible interface fit the one you expect
BridgeSplit a thing into two parts that can vary on their own
CompositeTreat one item and a group of items the same way
DecoratorAdd features to an object by wrapping it
FacadeOne simple front door for a complicated subsystem
FlyweightShare the common part of many similar objects
ProxyA stand-in that controls access to the real object

Behavioral patterns

How objects talk, share work and change behaviour at run time.

PatternWhat it does
Chain of ResponsibilityPass a request along handlers until one deals with it
CommandTurn a request into an object you can store, queue or undo
InterpreterRepresent a small language as classes and evaluate it
IteratorWalk through a collection without knowing how it is stored
MediatorLet objects talk through a middleman, not to each other
MementoSave a snapshot of an object so you can restore it later
ObserverTell everyone who is interested when something changes
StateAn object changes its behaviour when its state changes
StrategySwap an algorithm without changing the code that uses it
Template MethodFix the steps of a recipe, let subclasses fill in some of them
VisitorAdd new operations to a set of classes without editing them

Which ones to learn first

If you are short on time, learn the SOLID principles first, then Strategy, Observer, Factory Method, Decorator and Singleton. These appear most often in interviews and in real code.

See the full low-level design track, or read HLD vs LLD to see how this fits next to system design.