Design Patterns¶
The 23 patterns catalogued in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson and Vlissides, the "Gang of Four", grouped by what they are for.
Creational¶
How objects get made, so that creation logic does not leak into the code that uses them.
- Singleton Pattern, exactly one instance, globally reachable
- Factory Pattern, let subclasses decide which class to instantiate
- Abstract Factory Pattern, create whole families of related objects
- Builder Pattern, assemble a complex object step by step
- Prototype Pattern, create new objects by copying an existing one
Structural¶
How objects are composed into larger structures.
- Adapter Pattern, make an incompatible interface usable
- Bridge Pattern, split an abstraction from its implementation
- Composite Pattern, treat trees and leaves uniformly
- Decorator Pattern, add responsibilities at runtime, without subclassing
- Facade Pattern, one simple entry point over a complex subsystem
- Flyweight Pattern, share fine-grained objects to save memory
- Proxy Pattern, stand in for another object and control access to it
Behavioural¶
How objects communicate and distribute responsibility.
- Chain of Responsibility Pattern, pass a request along a chain of handlers
- Command Pattern, turn a request into an object you can queue and undo
- Interpreter Pattern, represent and evaluate a small language
- Iterator Pattern, traverse a collection without exposing it
- Mediator Pattern, replace a web of references with a hub
- Memento Pattern, snapshot and restore state without breaking encapsulation
- Observer Pattern, notify dependents automatically on change
- State Pattern, change behaviour when internal state changes
- Strategy Pattern, swap interchangeable algorithms at runtime
- Template Method Pattern, fix the skeleton, defer the steps
- Visitor Pattern, add operations to a hierarchy without changing it
Related¶
- Dependency Injection, not a GoF pattern, but the usual answer to the coupling these patterns fight
A word of warning¶
A pattern is a name for a solution to a recurring problem, not a goal. Applying one to a problem you do not have costs you indirection, classes and reading time, and buys nothing. Reach for a pattern when the pressure it relieves is already hurting.