The 23 Gang of Four design patterns, explained step by step
A free guide. Each page covers one pattern: the problem it solves, its solution, a diagram animated step by step and the same example written in Python, TypeScript, Java, C#, PHP and Go.
Start with Singleton Browse the catalog
- Python
- TypeScript
- Java
- C#
- PHP
- Go
- 23patterns
- 6languages
- 138code examples
- 160animated steps
A pattern in action: Observer
Define a subscription mechanism to automatically notify several objects of a state change. Press play or move forward step by step. The Observer page then walks through the code.
- Client
- Key role of the pattern
- Object
- Data or state
- Observers subscribe to the subject.
- StockListener โ Order [NEW]: subscribe()
- The subject's state changesโฆ
- โฆit notifies each subscriber, without knowing its class.
- Order [PAID] โ StockListener: update(order)
- New subscriber added without modifying Order (Open/Closed).
What is a design pattern?
A design pattern is a proven solution to a design problem that keeps coming up in object-oriented programming. It is not a piece of code to copy: it is a way of organizing classes and objects, with a name the whole team understands.
The 23 patterns in this guide come from the book Design Patterns: Elements of Reusable Object-Oriented Software (1994), written by four authors nicknamed the โGang of Fourโ (GoF). They are grouped into three families: creational, structural and behavioral.
-
Creational patterns
5 patterns
How to create objects flexibly, without coupling your code to concrete classes.
-
Structural patterns
7 patterns
How to assemble classes and objects into larger structures that stay flexible.
-
Behavioral patterns
11 patterns
How objects communicate and share responsibilities.
How to use this guide
- Read the problem before the solution: a pattern sticks when you remember the difficulty it removes.
- Step through the animated diagram to see which objects talk to each other, and in what order.
- Compare the example in your usual language, then in a second one.
The catalog
The pages follow one another in order, but each one can also be read on its own.
Creational patterns
How to create objects flexibly, without coupling your code to concrete classes.
- 1Singleton
Ensure a class has only one instance and provide a global access point to it.
- 2Factory Method
Define an interface for creating an object, but let subclasses decide which concrete class to instantiate.
- 3Abstract Factory
Provide an interface for creating families of related objects without specifying their concrete classes.
- 4Builder
Construct a complex object step by step, separating its construction from its representation.
- 5Prototype
Create new objects by cloning an existing instance, without depending on its concrete class.
Structural patterns
How to assemble classes and objects into larger structures that stay flexible.
- 6Adapter
Let objects with incompatible interfaces work together, through a translator.
- 7Bridge
Decouple an abstraction from its implementation so that the two can evolve independently.
- 8Composite
Compose objects into tree structures and treat leaves and groups uniformly.
- 9Decorator
Add behavior to an object dynamically by wrapping it in objects that share its interface.
- 10Facade
Provide a simplified interface to a complex set of classes, libraries or subsystems.
- 11Flyweight
Save memory by sharing the common (intrinsic) state across a large number of objects.
- 12Proxy
Provide a stand-in that controls access to an object: caching, lazy loading, security, loggingโฆ
Behavioral patterns
How objects communicate and share responsibilities.
- 13Chain of Responsibility
Pass a request along a chain of handlers: each one handles it, stops it, or forwards it to the next.
- 14Command
Turn a request into a standalone object: it can then be passed as a parameter, queued, logged or undone.
- 15Iterator
Traverse the elements of a collection without exposing its internal representation (list, tree, paginated APIโฆ).
- 16Mediator
Reduce chaotic dependencies between objects by forcing them to communicate through a central mediator object.
- 17Memento
Capture and later restore an object's internal state without violating its encapsulation.
- 18Observer
Define a subscription mechanism to automatically notify several objects of a state change.
- 19State
Let an object change its behavior when its internal state changes, as if it had changed class.
- 20Strategy
Define a family of interchangeable algorithms and let you choose which one to use at runtime.
- 21Template Method
Define the skeleton of an algorithm in a base class, letting subclasses override certain steps without changing its structure.
- 22Visitor
Separate an algorithm from the object structure it operates on, so you can add operations without modifying those classes.
- 23Interpreter
Define the grammar of a small language and an interpreter that evaluates sentences in that language.