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.
- Reuse Solutions
Utilize established designs to solve recurring problems, leveraging the experiences of others. - Establish Common Terminology
Design patterns create a shared vocabulary, improving communication during the analysis and design phases. - Higher-Level Perspective
Patterns allow developers to focus on design without getting bogged down by implementation details early.
Core Principles
- 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). - Composition over Inheritance
Instead of relying solely on inheritance, use composition (i.e., combine multiple objects) to achieve flexibility. - Delegation:
When a class passes responsibility to another class, this is called delegation. This enables efficient use of both inheritance and composition.
Categories
- Creational Patterns:
Focus on the creation of objects by abstracting the instantiation process. (Factory Method, Prototype) - Structural Patterns:
Deal with the organization of classes and objects to form larger structures. (Adapter, Decorator) - 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.