LOW LEVEL DESIGN · DESIGN PATTERNS

All 23 design patterns, explained with Java

A design pattern is a named, proven shape for a problem that keeps coming back. Learn the intent of each one, see it in a fresh Java example, and see which machine coding problems use it. Start with Strategy, Observer, State, Factory method and Builder: they show up the most in interviews.

Creational patterns

Five ways to create objects without hard-wiring which class you get.

PatternIn one lineUsed in
Singleton Exactly one shared instance, created once and reached from one place. Vending machine, Logger with multiple sinks
Factory method Let a method, often in a subclass, decide which concrete class to create. Splitwise, In-memory cache, Rate limiter
Abstract factory Create whole families of related objects that are guaranteed to match. —
Builder Assemble a complex object step by step, then validate it once in build(). Snakes and ladders
Prototype Make new objects by copying a configured one instead of building from scratch. —

Structural patterns

Seven ways to combine classes and objects into bigger structures that stay flexible.

PatternIn one lineUsed in
Adapter Wrap a class so its interface fits the one your code expects. —
Bridge Split two things that vary independently into two hierarchies joined by a reference. —
Composite Treat a single item and a group of items through the same interface. Rate limiter, In-memory file system, In-memory SQL-like database
Decorator Add behaviour by wrapping an object in another object with the same interface. Parking lot, Vehicle rental system, Circuit breaker
Facade Give a complicated subsystem one simple front door. Rate limiter
Flyweight Share the heavy, unchanging part of many small objects to save memory. —
Proxy Put a stand-in in front of an object to control access, cache or load lazily. Connection pool, Online auction

Behavioral patterns

Eleven ways objects share work, pass requests and react to change.

PatternIn one lineUsed in
Strategy Swap one algorithm for another at runtime behind one interface. Parking lot, Vending machine, Elevator system
Observer Notify every subscriber automatically when something changes. Parking lot, Vending machine, Elevator system
State Let an object change its behaviour by switching between state objects. Vending machine, Elevator system, Task scheduler
Command Turn a request into an object so it can be queued, logged or undone. In-memory file system, In-memory SQL-like database, Version control and undo/redo
Chain of responsibility Pass a request along a chain of handlers until it is dealt with. Logger with multiple sinks, ATM
Template method Fix the steps of an algorithm in a base class and let subclasses fill in some. Jigsaw puzzle
Iterator Walk through a collection without knowing how it is stored. In-memory cache
Mediator Make objects talk through one coordinator instead of to each other. —
Memento Save and restore an object's state without exposing its insides. Version control and undo/redo
Visitor Add new operations over a fixed set of classes without editing them. In-memory file system
Interpreter Represent a small language as classes and evaluate sentences in it. In-memory SQL-like database, Spreadsheet with formulas