Design Patterns are specific, reusable solutions to commonly occurring problems in software design. They are proven templates that can be applied to solve recurring design challenges.
What Makes Something a Design Pattern?
A design pattern:
- Solves a specific problem: Addresses a well-defined, recurring design challenge
- Is a proven solution: Has been validated through repeated successful use
- Is reusable: Can be applied in different contexts and situations
- Provides a template: Offers a structural blueprint, not finished code
- Has a name: Enables shared vocabulary among developers
Examples of design patterns:
- “Use the Strategy pattern to encapsulate algorithms and make them interchangeable”
- “Apply the Observer pattern to implement distributed event handling”
- “Follow GRASP patterns for assigning responsibilities to objects”
Gang of Four Design Patterns
The Gang of Four (GoF) catalog organizes 23 design patterns into three categories based on their purpose.
Decision Tree
Use this diagram to identify which pattern fits your problem:
flowchart TB Start{What kind of<br/>problem?} Start -->|Object creation| Creational[Creational Patterns] Start -->|Object structure| Structural[Structural Patterns] Start -->|Object behavior| Behavioral[Behavioral Patterns] Creational --> C1[Need families of related objects?] Creational --> C2[Too many constructor params?] Creational --> C3[Defer instantiation to subclasses?] Creational --> C4[Need to copy objects?] Creational --> C5[Need exactly one instance?] C1 --> AbstractFactory[Abstract Factory] C2 --> Builder[Builder] C3 --> FactoryMethod[Factory Method] C4 --> Prototype[Prototype] C5 --> Singleton[Singleton] Structural --> S1[Incompatible interfaces?] Structural --> S2[Decouple abstraction & implementation?] Structural --> S3[Treat parts & wholes uniformly?] Structural --> S4[Add responsibilities dynamically?] Structural --> S5[Simplify complex subsystem?] Structural --> S6[Share many fine-grained objects?] Structural --> S7[Control access to object?] S1 --> Adapter[Adapter] S2 --> Bridge[Bridge] S3 --> Composite[Composite] S4 --> Decorator[Decorator] S5 --> Facade[Facade] S6 --> Flyweight[Flyweight] S7 --> Proxy[Proxy] Behavioral --> B1[Multiple handlers for request?] Behavioral --> B2[Encapsulate request as object?] Behavioral --> B3[Access elements sequentially?] Behavioral --> B4[Decouple objects communication?] Behavioral --> B5[Capture & restore state?] Behavioral --> B6[Notify dependents of changes?] Behavioral --> B7[Behavior changes with state?] Behavioral --> B8[Encapsulate interchangeable algorithms?] Behavioral --> B9[Define algorithm skeleton?] Behavioral --> B10[Add operations without changing classes?] B1 --> ChainOfResp[Chain of Responsibility] B2 --> Command[Command] B3 --> Iterator[Iterator] B4 --> Mediator[Mediator] B5 --> Memento[Memento] B6 --> Observer[Observer] B7 --> State[State] B8 --> Strategy[Strategy] B9 --> TemplateMethod[Template Method] B10 --> Visitor[Visitor] click Creational "[[creational-patterns]]" click Structural "[[structural-patterns]]" click Behavioral "[[behavioral-patterns]]" click AbstractFactory "[[creational-patterns#abstract-factory|abstract-factory]]" click Builder "[[creational-patterns#builder|builder]]" click FactoryMethod "[[creational-patterns#factory-method|factory-method]]" click Prototype "[[creational-patterns#prototype|prototype]]" click Singleton "[[creational-patterns#singleton|singleton]]" click Adapter "[[structural-patterns#adapter|adapter]]" click Bridge "[[structural-patterns#bridge|bridge]]" click Composite "[[structural-patterns#composite|composite]]" click Decorator "[[structural-patterns#decorator|decorator]]" click Facade "[[structural-patterns#facade|facade]]" click Flyweight "[[structural-patterns#flyweight|flyweight]]" click Proxy "[[structural-patterns#proxy|proxy]]" click ChainOfResp "[[behavioral-patterns#chain-of-responsibility|chain-of-responsibility]]" click Command "[[behavioral-patterns#command|command]]" click Iterator "[[behavioral-patterns#iterator|iterator]]" click Mediator "[[behavioral-patterns#mediator|mediator]]" click Memento "[[behavioral-patterns#memento|memento]]" click Observer "[[behavioral-patterns#observer|observer]]" click State "[[behavioral-patterns#state|state]]" click Strategy "[[behavioral-patterns#strategy|strategy]]" click TemplateMethod "[[behavioral-patterns#template-method|template-method]]" click Visitor "[[behavioral-patterns#visitor|visitor]]" style Start fill:#f9d5d5,stroke:#d14,stroke-width:3px style Creational fill:#e8d4ff,stroke:#9333ea,stroke-width:2px style Structural fill:#ccf2f2,stroke:#0891b2,stroke-width:2px style Behavioral fill:#ffe8cc,stroke:#d97706,stroke-width:2px
Pattern Categories
- Creational Patterns - Object creation mechanisms
- Structural Patterns - Object composition and relationships
- Behavioral Patterns - Object collaboration and responsibilities
Classic Design Patterns
Behavioral Patterns
Strategy Pattern
Encapsulates algorithms and makes them interchangeable. Lets the algorithm vary independently from clients that use it.
Resources:
Related: Composition over Inheritance
Observer Pattern
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified automatically.
Resources:
Object-Oriented Design Patterns
GRASP (General Responsibility Assignment Software Patterns)
Nine fundamental principles for assigning responsibilities to classes and objects in object-oriented design:
- Information Expert
- Creator
- Controller
- Low Coupling
- High Cohesion
- Polymorphism
- Pure Fabrication
- Indirection
- Protected Variations
Resources:
How Patterns Relate to Principles and Concepts
Design patterns are more specific than design principles. While principles provide general guidelines (like “favor composition over inheritance”), patterns provide concrete solutions (like “use the Strategy pattern to implement this compositional relationship”).
Patterns often embody multiple principles:
- Strategy pattern embodies Composition over Inheritance
- Observer pattern embodies Closed Principle
- GRASP patterns directly apply design concepts like coupling and cohesion
Patterns vs. Principles vs. Concepts
| Category | Purpose | Example |
|---|---|---|
| Concepts | Explain WHY | Coupling, cohesion, invariants |
| Principles | Guide HOW | Composition over Inheritance, Tell, Don’t Ask |
| Patterns | Provide WHAT | Strategy, Observer, GRASP |
See Also
- Design Principles - Guidelines that patterns embody
- Design Concepts - Theoretical foundations
- Architectural Patterns - Higher-level structural patterns
- Good Software Practices