What criteria should guide pattern selection?
Answer
Pattern selection should be driven by the specific problem you face, not by a desire to use patterns. Key criteria: (1) Variability — what aspect of the design is likely to change? If the algorithm varies, use Strategy. If the object's state varies, use State. If the family of products varies, use Abstract Factory. (2) Coupling — do you need to decouple object creation (Creational), structure (Structural), or communication (Behavioral)? (3) Extension direction — do you need to add new operations without changing objects (Visitor) or add new objects without changing operations (Strategy)? (4) Team familiarity — a well-known pattern communicates intent immediately to any developer who recognizes it; an obscure pattern adds cognitive load. (5) Testability — patterns that rely on composition (Strategy, Observer) are generally more testable than those that rely on inheritance (Template Method) or global state (Singleton).
Previous
How are GoF patterns used in modern frameworks like Spring and Django?
Next
How do GoF patterns apply in functional programming?
More Design Patterns (Gang of Four) Questions
View all →- Advanced How do you implement a thread-safe Singleton using double-checked locking?
- Advanced How do the SOLID principles relate to GoF patterns?
- Advanced What is the difference between GoF anti-patterns and the patterns themselves?
- Advanced How are GoF patterns used in modern frameworks like Spring and Django?
- Advanced How do GoF patterns apply in functional programming?