Design Patterns


A design pattern is a reusable solution to a commonly occurring problem in software design. It provides a template or guideline to address specific challenges, helping developers avoid reinventing the wheel.

Core Principles

  1. Program to an Interface, not an Implementation
    Decouples design from implementation, making it easier to swap implementations (e.g., use a mock database during testing).
  2. Composition over Inheritance
    Instead of relying solely on inheritance, use composition (i.e., combine multiple objects) to achieve flexibility.
  3. Delegation:
    When a class passes responsibility to another class, this is called delegation. This enables efficient use of both inheritance and composition.

Categories

  1. Creational Patterns:
    Focus on the creation of objects by abstracting the instantiation process. (Factory Method, Prototype)
  2. Structural Patterns:
    Deal with the organization of classes and objects to form larger structures. (Adapter, Decorator)
  3. Behavioral Patterns:
    Concerned with the interaction and communication between objects and the assignment of responsibilities. (Strategy, Observer, Chain of Responsibility)

Design Patterns vs. Frameworks

Design patterns are conceptual solutions, promoting best practices across various projects and technologies, while frameworks are pre-built systems that enforce a certain way of working. Patterns offer flexibility, but frameworks trade that for faster development with some restrictions on design choices.