Here’s a comprehensive list of design patterns, categorized into the three main types: Creational, Structural, and Behavioral. These patterns, popularized by the “Gang of Four” (GoF) book Design Patterns: Elements of Reusable Object-Oriented Software (1994), along with some additional widely recognized patterns, are commonly used in software engineering to solve recurring design problems.
1. Creational Design Patterns
These patterns deal with object creation mechanisms, aiming to create objects in a manner suitable to the situation.
- Singleton
- Ensures a class has only one instance and provides a global point of access to it.
- Example: Logger, Configuration Manager.
- Factory Method
- Defines an interface for creating an object but lets subclasses decide which class to instantiate.
- Example: Creating different types of documents in a word processor.
- Abstract Factory
- Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- Example: GUI toolkit creating buttons and scrollbars for different OS.
- Builder
- Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
- Example: Building a complex SQL query or a meal combo.
- Prototype
- Creates new objects by copying an existing object (prototype), avoiding the need for subclassing.
- Example: Cloning a template document.
- Object Pool(Not in GoF)
- Manages a pool of reusable objects to reduce the overhead of creating and destroying them.
- Example: Database connection pooling.
2. Structural Design Patterns
These patterns focus on how classes and objects are composed to form larger structures while keeping them flexible and efficient.
- Adapter
- Converts the interface of a class into another interface that a client expects.
- Example: Adapting a legacy system to a new API.
- Bridge
- Decouples an abstraction from its implementation so the two can vary independently.
- Example: Separating a shape’s drawing logic from its rendering engine.
- Composite
- Composes objects into tree-like structures to represent part-whole hierarchies, treating individual objects and compositions uniformly.
- Example: File system (files and directories).
- Decorator
- Attaches additional responsibilities to an object dynamically, providing a flexible alternative to subclassing.
- Example: Adding scrollbars or borders to a window.
- Facade
- Provides a simplified interface to a complex subsystem.
- Example: A single API for a multimedia library.
- Flyweight
- Reduces memory usage by sharing as much data as possible with similar objects.
- Example: Reusing font objects in a text editor.
- Proxy
- Provides a surrogate or placeholder for another object to control access to it.
- Example: Lazy loading an image via a proxy.
3. Behavioral Design Patterns
These patterns focus on communication between objects, defining how they interact and distribute responsibilities.
- Chain of Responsibility
- Passes a request along a chain of handlers, allowing any handler to process it or pass it on.
- Example: Event handling in a GUI.
- Command
- Encapsulates a request as an object, allowing parameterization and queuing of requests.
- Example: Undo/redo operations in an editor.
- Interpreter
- Defines a grammar for a language and provides an interpreter to evaluate sentences in that language.
- Example: SQL or regular expression parsing.
- Iterator
- Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.
- Example: Iterating over a list or set.
- Mediator
- Defines an object that centralizes communication between a set of objects, reducing their dependencies.
- Example: Chat room coordinating messages.
- Memento
- Captures and externalizes an object’s internal state so it can be restored later without violating encapsulation.
- Example: Saving game state.
- Observer
- Defines a one-to-many dependency where multiple objects (observers) are notified of state changes in another object (subject).
- Example: Event listeners in UI frameworks.
- State
- Allows an object to alter its behavior when its internal state changes, appearing as if it changes its class.
- Example: Vending machine states (idle, dispensing).
- Strategy
- Defines a family of algorithms, encapsulates each one, and makes them interchangeable within that family.
- Example: Sorting algorithms (bubble sort, quicksort).
- Template Method
- Defines the skeleton of an algorithm in a method, deferring some steps to subclasses.
- Example: Framework hooks for customization.
- Visitor
- Represents an operation to be performed on elements of an object structure, allowing new operations without modifying the structure.
- Example: Traversing a syntax tree for different operations (e.g., type checking, code generation).
Additional Notable Patterns (Beyond GoF)
These patterns have gained prominence in modern software design:
- Dependency Injection
- Provides dependencies to a class from an external source rather than having the class create them itself.
- Example: Spring IoC container.
- Null Object
- Uses a special object to represent “do nothing” behavior instead of returning null, avoiding null checks.
- Example: A default logger that discards messages.
- Repository
- Mediates between the domain and data mapping layers, acting like an in-memory collection of domain objects.
- Example: Data access layer in ORM frameworks.
- MVC (Model-View-Controller)
- Separates application logic into three interconnected components: Model (data), View (UI), and Controller (input handling).
- Example: Web frameworks like Django or Rails.
Summary Table
Category | Patterns |
---|---|
Creational | Singleton, Factory Method, Abstract Factory, Builder, Prototype, Object Pool |
Structural | Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy |
Behavioral | Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor |
Additional | Dependency Injection, Null Object, Repository, MVC |
This list covers the foundational GoF patterns and some widely adopted extras. Each pattern addresses specific problems, and their applicability depends on the context of your design.