Design Patterns Cheat Sheet

In Design

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.

PatternDescriptionExample
SingletonEnsures that only one instance of a class is created and provides a global point of access to it.Database connection manager
Factory MethodDefines an interface for creating objects, but allows subclasses to decide which class to instantiate.Document reader factory
Abstract FactoryProvides an interface for creating families of related or dependent objects without specifying their concrete classes.GUI toolkit factory
BuilderSeparates the construction of a complex object from its representation, allowing the same construction process to create different representations.HTML page builder
PrototypeCreates 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.

PatternDescriptionExample
AdapterConverts the interface of a class into another interface that clients expect.Legacy printer adapter
BridgeSeparates an object’s interface from its implementation, allowing them to vary independently.Drawing program with different rendering engines
CompositeComposes objects into tree structures to represent part-whole hierarchies.File system directory structure
DecoratorDynamically adds responsibilities to an object by wrapping it in a decorator object.Text editor with spell checking
FacadeProvides a simplified interface to a complex subsystem.Operating system API
FlyweightShares objects to support large numbers of fine-grained objects efficiently.Text editor with shared font objects
ProxyProvides 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.

PatternDescriptionExample
Chain of ResponsibilityAllows multiple objects to handle a request, passing it along a chain until one of them handles it.Event handling in a GUI toolkit
CommandEncapsulates a request as an object, allowing it to be queued, logged, and undone.Text editor undo/redo stack
InterpreterDefines a grammar for a language and provides an interpreter to parse and execute expressions in that language.Regular expression parser
IteratorProvides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.Collection traversal
MediatorDefines an object that encapsulates how a set of objects interact, allowing them to be decoupled from each other.Chat room mediator
MementoCaptures and externalizes an object’s internal state, allowing it to be restored later.Game state save/restore
ObserverDefines 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
StateAllows an object to alter its behavior when its internal state changes.Traffic light controller
StrategyDefines a family of algorithms, encapsulates each one, and makes them interchangeable.Sorting algorithm selection
Template MethodDefines the skeleton of an algorithm in a method, deferring some steps to subclasses.Web page template
VisitorSeparates 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