Pattern | What It Does | Problem It Solves |
---|---|---|
Singleton | Restricts a class to one instance. | a shared global resource that is created lazily / from cache / async way |
Factory Method | Delegates object creation to subclasses. | You need different objects based on runtime context without altering client code. |
Prototype | Clones existing objects. | duplicate objects without depending on their constructors. |
Builder | Builds complex objects step-by-step. | objects with many optional fields/ configurations (like pizzas or HTTP requests). |
Abstract Factory | Creates related object families without knowing concrete types. | You need consistent object families (e.g., UI kits) across multiple systems. |
Pattern | What It Does | Problem It Solves |
---|---|---|
Adapter | Makes incompatible interfaces work together. | a new module that doesn’t match your system’s existing interface. |
Facade | Simplifies access to a complex system. | a clean interface over messy or verbose |
Decorator | Adds behavior dynamically to an object. | modify objects at runtime without altering existing code. |
Proxy | Controls access to another object. | You need lazy-loading, security layers, or smart caching around objects. |
Composite | Treats group of objects as a single one. | You’re working with hierarchies like trees and need uniform access. |
Flyweight | Shares data across many similar objects. | You’re creating thousands of similar objects with shared state (like icons). |
Bridge | Separates abstraction from implementation. | You need flexibility to change abstraction or implementation independently. |
Pattern | What It Does | Problem It Solves |
---|---|---|
Iterator | Provides traversal over a collection. | uniform access across various data structures (like lists or trees). |
Strategy | Selects algorithm dynamically. | You have multiple interchangeable algorithms (e.g., sorting, payment methods). |
Observer | Alerts multiple objects of changes. | You need real-time updates between objects (like UI reacting to model changes). |
Template Method | Outlines algorithm steps, some overridden. | You need shared logic flow with customizable parts for subclasses. |
Command | Encapsulates requests as objects. | You need to queue, log, or undo operations. |
Chain of Responsibility | Passes requests through handlers until one handle it. | You need loosely coupled processing steps (e.g., middleware). |
State | Alters behavior based on internal state. | You want objects to behave differently as their state changes (like a TCP connection). |
Memento | Saves and restores object state. | You need undo/rollback functionality without violating encapsulation. |
Mediator | Centralizes complex communication. | You have tightly coupled objects that need streamlined communication. |
Visitor | Adds new behavior to object structures. | You want to extend operations on a data structure without changing its classes. |