Design Patterns
Reusable solutions to common problems in software design, providing a shared vocabulary and proven approaches for recurring design challenges.
Detailed Explanation
Design patterns are not code—they are templates for solving common problems. The original "Gang of Four" patterns (1994) define 23 patterns in three categories: Creational (how to create objects: Singleton, Factory, Builder), Structural (how to compose objects: Adapter, Decorator, Proxy), and Behavioral (how objects communicate: Observer, Strategy, Command).
Modern patterns: Repository (data access abstraction), Dependency Injection (inverting control), MVC/MVVM (separation of concerns), and Event Sourcing (storing events instead of state). Patterns should be applied judiciously—not every problem needs a pattern, and over-engineering with patterns can make code harder to understand.
Why It Matters
Design patterns provide proven solutions to common problems and a shared vocabulary for developers. Knowing patterns helps you recognize and solve design challenges.
Real-World Example
A developer uses the Strategy pattern to implement multiple payment methods (credit card, PayPal, crypto). Each payment method is a separate class implementing the same interface, making it easy to add new payment options.
When to Use
When you encounter a recurring design problem. Don't force patterns into code where they don't fit. Apply patterns when they simplify the design, not when they add complexity.
Advantages
- Proven solutions to common problems
- Shared vocabulary for developers
- Encapsulate variation and promote flexibility
- Reduce development time with tested approaches
- Improve code organization and maintainability
Disadvantages
- Can be over-engineered for simple problems
- Add complexity and abstraction layers
- May not fit modern frameworks
- Learning curve for beginners
- Can become cargo-cult programming
Related Terms
Frequently Asked Questions
Should I learn all 23 Gang of Four patterns?
No. Learn the most commonly used: Singleton, Factory, Builder, Adapter, Decorator, Observer, Strategy, and Command. Understand the problems they solve and apply them when appropriate.
Are design patterns still relevant?
Yes, but many patterns are now built into modern frameworks (dependency injection, MVC, observer). Understanding the patterns helps you use the frameworks effectively and recognize when a pattern is needed.
What is the difference between a pattern and an anti-pattern?
A pattern is a proven solution to a common problem. An anti-pattern is a common practice that seems useful but leads to bad outcomes (God object, spaghetti code, golden hammer). Learn both to recognize good and bad design.
How do I know when to apply a pattern?
When you recognize the problem the pattern solves: needing to vary behavior (Strategy), creating objects without specifying concrete classes (Factory), or adding responsibilities dynamically (Decorator). Don't apply patterns preemptively.
What is dependency injection?
Dependency Injection (DI) is a pattern where objects receive their dependencies from outside rather than creating them internally. It promotes loose coupling, testability, and flexibility. Most modern frameworks have built-in DI containers.