Design patterns are categorized into three main types: Creational, Structural, and Behavioral. Each category addresses different aspects of software design problems and provides templates for solving these problems. Here’s an overview of all design patterns within these categories along with simple use cases.
Creational Patterns
Creational patterns deal with object creation mechanisms, enhancing flexibility and reuse.
Singleton: Ensures a class has only one instance and provides a global access point.
- Use case: A configuration manager in an application where only one instance should manage the configuration settings.
Factory Method: Defines an interface for creating an object but lets subclasses alter the type of objects that will be created.
- Use case: Creating different types of documents (e.g., Word, PDF) in an application without changing the code that uses these documents.
Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- Use case: Creating UI components (e.g., buttons, checkboxes) for different operating systems without altering the client code.
Builder: Separates the construction of a complex object from its representation.
- Use case: Building a complex House object with various parts like rooms, doors, and windows, step by step.
Prototype: Specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype.
- Use case: Creating new instances of a customizable character in a game by cloning a prototype character.
Structural Patterns
Structural patterns deal with object composition, ensuring that changes in one part do not affect the entire system.
Adapter: Allows incompatible interfaces to work together.
- Use case: Integrating a legacy system with a new system where the interfaces are different.
Bridge: Separates an object’s abstraction from its implementation, allowing the two to vary independently.
- Use case: Separating the interface and implementation of a remote control and different devices like TV and Radio.
Composite: Composes objects into tree structures to represent part-whole hierarchies.
- Use case: Representing a file system where directories contain files and other directories.
Decorator: Adds behavior to objects dynamically.
- Use case: Adding scrolling or borders to windows in a GUI application without modifying the original window class.
Facade: Provides a simplified interface to a complex subsystem.
- Use case: Simplifying interaction with a complex library (e.g., multimedia library) by providing a unified interface.
Flyweight: Reduces the cost of creating and manipulating a large number of similar objects.
- Use case: Rendering a large number of characters in a text editor, sharing common character data.
Proxy: Provides a surrogate or placeholder for another object to control access to it.
- Use case: Implementing lazy initialization, access control, or logging by using a proxy object.
Behavioral Patterns
Behavioral patterns are concerned with communication between objects and responsibilities of objects.
Chain of Responsibility: Passes a request along a chain of handlers.
- Use case: Handling a request in a web application where the request can be processed by different handlers (e.g., authentication, logging).
Command: Encapsulates a request as an object, allowing parameterization of clients with queues and requests.
- Use case: Implementing undo functionality in a text editor where each action (e.g., typing, deleting) is encapsulated as a command.
Interpreter: Implements a specialized language interpreter for a defined grammar.
- Use case: Parsing and evaluating expressions in a calculator application.
Iterator: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Use case: Iterating over elements of a collection (e.g., list, set) in a uniform way.
Mediator: Defines an object that encapsulates how a set of objects interact.
- Use case: Managing communication between different UI components (e.g., dialog box, buttons) in a GUI application.
Memento: Captures and externalizes an object’s internal state without violating encapsulation, allowing the object to be restored to this state later.
- Use case: Implementing save and restore functionality in a text editor.
Observer: Defines a one-to-many dependency so that when one object changes state, all its dependents are notified.
- Use case: Implementing a news subscription system where users get updates when a news article is published.
State: Allows an object to alter its behavior when its internal state changes.
- Use case: Implementing different behaviors for a vending machine depending on its current state (e.g., has money, no money).
Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
- Use case: Implementing different sorting algorithms (e.g., bubble sort, merge sort) that can be selected at runtime.
Template Method: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses.
- Use case: Defining a template for a game where the main algorithm is fixed but some steps (e.g., starting and ending the game) can be customized.
Visitor: Represents an operation to be performed on the elements of an object structure, allowing new operations to be defined without changing the classes of the elements on which it operates.
- Use case: Performing operations (e.g., printing, validating) on elements of a document object model (DOM) structure.
These patterns help create flexible, reusable, and maintainable code by providing proven solutions to common design problems.