Design patterns
Solutions to recurring design problems: how they work, when to apply them, and when a simpler alternative is the better call.
28 guides
- What are design patterns?
What makes a solution a design pattern, how the classic catalog is organized, and why every pattern has to be adapted to the problem.
Creational patterns
- Creational patterns
The way objects are created shapes the design too. How to separate their construction from the code that uses them, and how to pick the right pattern.
- Factory Method
How to delegate the choice of which object gets created to subclasses, without coupling the calling code to a concrete class.
- Abstract Factory
How to create families of compatible objects without depending on their concrete classes, and keep that compatibility when the implementation changes.
- Builder
How to construct objects step by step when a constructor with many parameters becomes hard to use and maintain.
- Prototype
Creating objects from existing ones can simplify their construction. How cloning works and what it demands care with.
- Singleton
How to guarantee a single instance, and what offering global access to it means for dependencies and tests.
Structural patterns
- Structural patterns
How to combine classes and objects to adapt interfaces, extend behavior, and simplify the use of complex components.
- Adapter
How to integrate components with incompatible interfaces through an adapter, without modifying their implementations.
- Bridge
How to separate an abstraction from its implementation so both can evolve without creating a subclass for every combination.
- Composite
How to work with individual objects and groups of objects through one interface, simplifying how hierarchical structures are handled.
- Decorator
How to add behavior to an object through composition and combine extensions without creating a subclass for every variant.
- Facade
How to offer a simple interface over a complex subsystem so its clients don't have to coordinate every component.
- Flyweight
When many objects share information, storing it once can cut memory use. How to separate shared state from the state that depends on each context.
- Proxy
How to control access to an object through a stand-in with the same interface, and when that turns out to be useful.
Behavioral patterns
- Behavioral patterns
How to distribute responsibilities and coordinate objects without making their interactions hard to maintain.
- Chain of Responsibility
How to process a request through a chain of handlers and decouple the sender from whoever can handle it.
- Command
How to represent actions as objects so you can queue them, log them, or allow undoing them, separated from the code that invokes them.
- Interpreter
How to represent the rules of a simple language as objects and combine them to interpret expressions.
- Iterator
How to traverse a collection without exposing its internal structure or coupling the traversal to a concrete implementation.
- Mediator
How to centralize coordination between objects to reduce their direct dependencies and simplify their interactions.
- Memento
How to save and restore an object's state without exposing its internal details.
- Observer
How to notify subscribed objects of changes without depending on their concrete classes, and what that relationship means for the design.
- State
How to organize an object's behavior by its state and make the allowed transitions explicit.
- Strategy
How to encapsulate variants of an algorithm and swap them without modifying the code that uses them.
- Template Method
How to define an algorithm's sequence in a base class and let subclasses adapt specific steps.
- Visitor
How to add operations over a structure of objects without modifying their classes each time, and where this approach runs out.
Comparisons and reference
- Design patterns that are often confused
Some patterns have similar structures but solve different problems. What sets them apart and how to recognize when to use each.