# Design Patterns Design patterns are reusable solutions to commonly occurring problems in software design, popularized by the "Gang of Four" (GoF) book. ## Creational Patterns How objects are created. - **Singleton** — ensures a class has only one instance with a global access point (e.g., connection pool, logger) - **Factory Method** — defines an interface for creating objects, letting subclasses decide the class to instantiate - **Abstract Factory** — creates families of related objects without specifying concrete classes - **Builder** — constructs complex objects step by step, separating construction from representation - **Prototype** — creates new objects by cloning an existing object ## Structural Patterns How objects are composed into larger structures. - **Adapter** — converts one interface into another clients expect - **Decorator** — adds responsibilities dynamically by wrapping objects (e.g., `BufferedReader(FileReader(file))`) - **Facade** — provides a simplified interface to a complex subsystem - **Proxy** — surrogate controlling access to another object (lazy loading, access control, logging) - **Composite** — tree structures where clients treat individual objects and compositions uniformly - **Bridge** — separates abstraction from implementation so they vary independently - **Flyweight** — shares common state among many objects to save memory ## Behavioral Patterns How objects communicate and distribute responsibility. - **Observer** — one-to-many dependency; when one changes, all dependents are notified - **Strategy** — family of interchangeable algorithms encapsulated individually - **Command** — encapsulates requests as objects, enabling queuing, undo, and parameterization - **Iterator** — sequential access without exposing underlying representation - **Template Method** — skeleton algorithm in superclass; subclasses override specific steps - **State** — object changes behavior when internal state changes - **Chain of Responsibility** — passes request along a chain of handlers - **Mediator** — centralizes complex communication between objects - **Memento** — captures and restores object state without violating encapsulation - **Visitor** — adds operations to object structures without modifying them --- Reference: [Refactoring Guru](https://refactoring.guru/design-patterns) See also: [[SOLID]], [[Composition vs Inheritance]], [[OOP]]