Design patterns are reusable solutions to common software design problems. They are a way to solve problems that have been encountered before, and have been proven to work. Design patterns are not specific to any programming language or technology, but rather are general solutions that can be applied to any software system.
Design patterns are important because they help developers create software that is more maintainable, scalable, and flexible. By using design patterns, developers can avoid common mistakes and pitfalls, and can create software that is easier to understand and modify.
This cheat sheet provides an overview of common design patterns used in software development. Each pattern is listed with a brief description and an example of its use.
Creational Patterns
Creational patterns are used to create objects in a way that is flexible and reusable.
Pattern | Description | Example |
---|---|---|
Singleton | Ensures that only one instance of a class is created and provides a global point of access to it. | Database connection manager |
Factory Method | Defines an interface for creating objects, but allows subclasses to decide which class to instantiate. | Document reader factory |
Abstract Factory | Provides an interface for creating families of related or dependent objects without specifying their concrete classes. | GUI toolkit factory |
Builder | Separates the construction of a complex object from its representation, allowing the same construction process to create different representations. | HTML page builder |
Prototype | Creates new objects by cloning an existing object, rather than creating a new one from scratch. | Game character cloning |
Structural Patterns
Structural patterns are used to organize classes and objects into larger structures.
Pattern | Description | Example |
---|---|---|
Adapter | Converts the interface of a class into another interface that clients expect. | Legacy printer adapter |
Bridge | Separates an object’s interface from its implementation, allowing them to vary independently. | Drawing program with different rendering engines |
Composite | Composes objects into tree structures to represent part-whole hierarchies. | File system directory structure |
Decorator | Dynamically adds responsibilities to an object by wrapping it in a decorator object. | Text editor with spell checking |
Facade | Provides a simplified interface to a complex subsystem. | Operating system API |
Flyweight | Shares objects to support large numbers of fine-grained objects efficiently. | Text editor with shared font objects |
Proxy | Provides a surrogate or placeholder for another object to control access to it. | Remote object proxy |
Behavioral Patterns
Behavioral patterns are used to manage algorithms, relationships, and responsibilities between objects.
Pattern | Description | Example |
---|---|---|
Chain of Responsibility | Allows multiple objects to handle a request, passing it along a chain until one of them handles it. | Event handling in a GUI toolkit |
Command | Encapsulates a request as an object, allowing it to be queued, logged, and undone. | Text editor undo/redo stack |
Interpreter | Defines a grammar for a language and provides an interpreter to parse and execute expressions in that language. | Regular expression parser |
Iterator | Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. | Collection traversal |
Mediator | Defines an object that encapsulates how a set of objects interact, allowing them to be decoupled from each other. | Chat room mediator |
Memento | Captures and externalizes an object’s internal state, allowing it to be restored later. | Game state save/restore |
Observer | Defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically. | Model-view-controller architecture |
State | Allows an object to alter its behavior when its internal state changes. | Traffic light controller |
Strategy | Defines a family of algorithms, encapsulates each one, and makes them interchangeable. | Sorting algorithm selection |
Template Method | Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. | Web page template |
Visitor | Separates an algorithm from an object structure by moving the algorithm into a separate object. | Document element visitor |
References
- Design Patterns: Elements of Reusable Object-Oriented Software, Gamma et al. (1994)
- Head First Design Patterns, Freeman et al. (2004)
- Refactoring Guru, https://refactoring.guru/design-patterns