Singleton Pattern
| When a single instance of a class is needed to coordinate actions across the system.
| A configuration manager that reads settings from a file and provides a global point of access to these settings throughout an application.
|
---|
Factory Method Pattern
| When a class can't anticipate the type of objects it needs to create beforehand.
| A logging framework that creates different loggers (e.g., file logger, console logger) based on configuration settings.
|
---|
Abstract Factory Pattern
|
When the system needs to be independent of how its objects are created, composed, and represented.
|
A GUI toolkit that supports multiple look-and-feel standards (e.g., Windows, macOS, Linux) and uses different factories to create the appropriate UI components.
|
---|
Builder Pattern
|
When an object needs to be constructed by a series of steps, and the construction process must allow different representations.
|
A complex document generator (e.g., HTML, PDF) where the construction process involves assembling various components in specific formats.
|
---|
Prototype Pattern
|
When the cost of creating a new instance of a class is more expensive than copying an existing instance.
|
A graphic editor where new shapes (e.g., circles, squares) can be created by cloning existing shapes, allowing for quick duplication with minor modifications.
|
---|
Adapter Pattern
|
When you need to integrate a new component with an existing system whose interface is incompatible.
|
Integrating a new payment gateway into an existing e-commerce application by using an adapter to convert the new gateway's API to match the expected interface.
|
---|
Bridge Pattern
|
When you want to separate an object's abstraction from its implementation so that the two can vary independently.
| A graphical application that separates the abstraction of shapes (e.g., circles, squares) from their rendering implementations (e.g., raster, vector).
|
---|
Composite Pattern
| When you need to treat individual objects and compositions of objects uniformly.
|
A file system where files and directories are represented as objects, and directories can contain files or other directories.
|
---|
Decorator Pattern
|
When you want to add responsibilities to individual objects dynamically and transparently, without affecting other objects.
|
Adding scrollbars or borders to windows in a GUI toolkit, where these additional features are represented as decorators.
|
---|
Facade Pattern
|
When you want to provide a simplified interface to a complex subsystem.
|
A hotel booking system where a facade provides a simplified interface to check room availability, book rooms, and manage payments, hiding the complex interactions between various subsystems.
|
---|
Flyweight Pattern
|
When you need to support a large number of fine-grained objects efficiently by sharing as much data as possible.
|
A text editor where each character is a flyweight object that shares common properties (e.g., font style, size) to reduce memory usage.
|
---|
Proxy Pattern
|
When you need a placeholder for another object to control access, reduce cost, or add additional functionality.
|
A virtual proxy in a document editor that loads large images on demand rather than at the initial document load, improving performance.
|
---|
Chain of Responsibility Pattern
|
When multiple objects might handle a request, but the handler is determined at runtime.
|
A logging framework where different loggers (e.g., console logger, file logger) form a chain, each handling the log request based on its level (info, warning, error).
|
---|
Command Pattern
|
When you need to parameterize objects with operations, queue operations, and support undoable operations.
|
A text editor where user actions (e.g., typing, deleting) are encapsulated as command objects that can be undone or redone.
|
---|
Interpreter Pattern
|
When you need to interpret a language and the language's grammar is simple and stable.
|
A simple calculator that interprets and evaluates mathematical expressions entered by the user.
|
---|
Iterator Pattern
|
When you need a way to access elements of a collection sequentially without exposing its underlying representation.
|
A collection of books in a library where an iterator provides a way to traverse the book collection without exposing the internal storage details.
|
---|
Mediator Pattern
|
When you want to reduce the complexity of communication between multiple objects by centralizing the interactions.
|
A chat application where a mediator object manages the communication between different user objects, ensuring that each user receives messages from others.
|
---|
Memento Pattern
|
When you need to capture and restore an object's internal state without violating encapsulation.
|
An undo feature in a drawing application where the state of the drawing is saved at various points, allowing the user to revert to a previous state.
|
---|
Observer Pattern
|
When one object changes state, all its dependents are notified and updated automatically.
|
A news agency where subscribers (observers) receive updates whenever a new article is published.
|
---|
State Pattern
|
When an object's behavior changes based on its state, and it must change its behavior at runtime.
|
A media player where the play, pause, and stop actions vary depending on the current state of the player (playing, paused, stopped).
|
---|
Strategy Pattern
|
When you need to define a family of algorithms, encapsulate each one, and make them interchangeable.
|
A sorting component in a software library that can use different sorting algorithms (e.g., quicksort, mergesort) interchangeably based on the context.
|
---|
Template Method Pattern
|
When you need to define the skeleton of an algorithm in a method, deferring some steps to subclasses.
|
A data processing application where the steps for processing data (e.g., read, process, write) are defined in a template method, but the specific processing logic is implemented in subclasses.
|
---|
Visitor Pattern
| When you need to perform operations on objects of a complex object structure without changing the classes on which it operates.
| A compiler where the visitor pattern is used to perform operations (e.g., type checking, code generation) on nodes of an abstract syntax tree.
|
---|