Frequently Asked Questions
Answers to common questions about design patterns, based on Design Patterns for Humans.General Questions
What are design patterns?
What are design patterns?
Are design patterns a silver bullet?
Are design patterns a silver bullet?
Should I try to use design patterns everywhere in my code?
Should I try to use design patterns everywhere in my code?
What happens if I use patterns in the wrong place?
What happens if I use patterns in the wrong place?
- Unnecessary complexity
- Harder to understand code
- More difficult maintenance
- Over-engineered solutions
- Confused team members
- Reduced code flexibility
What are the three main categories of design patterns?
What are the three main categories of design patterns?
- Creational Patterns: Focused on how to instantiate an object or group of related objects. They deal with object creation mechanisms.
- Structural Patterns: Concerned with object composition - how entities can use each other. They help answer “How to build a software component?”
- Behavioral Patterns: Concerned with assignment of responsibilities between objects. They outline patterns for message passing and communication. They help answer “How to run a behavior in software component?”
How many design patterns are there?
How many design patterns are there?
- 6 Creational patterns (including Simple Factory)
- 7 Structural patterns
- 10 Behavioral patterns
Creational Patterns
What's the difference between Simple Factory, Factory Method, and Abstract Factory?
What's the difference between Simple Factory, Factory Method, and Abstract Factory?
- Simply generates an instance without exposing instantiation logic
- Use when creating an object involves some logic beyond simple assignments
- Not actually part of the original GoF patterns
- Delegates the instantiation logic to child classes
- Use when there’s generic processing but the required sub-class is dynamically decided at runtime
- The client doesn’t know what exact sub-class it needs
- A factory of factories - groups related/dependent factories together
- Use when there are interrelated dependencies with complex creation logic
- Creates families of related objects
When should I use Builder vs Factory?
When should I use Builder vs Factory?
- Creation is a one-step process
- The object can be created and returned immediately
- Example: Making different types of doors
- Creation is a multi-step process
- You need to avoid the telescoping constructor anti-pattern
- There could be several flavors of an object
- You want to construct an object step by step
- Example: Building a customized burger with many optional toppings
Is Singleton an anti-pattern? Should I avoid it?
Is Singleton an anti-pattern? Should I avoid it?
- Introduces global state in your application
- Makes your code tightly coupled
- Makes mocking difficult in tests
- Can hide dependencies
- Can cause issues in multi-threaded environments
- When exactly one object is needed to coordinate actions across the system
- For things like logging, driver objects, caching, etc.
When should I use Prototype pattern?
When should I use Prototype pattern?
- An object is required that is similar to an existing object
- The creation would be expensive compared to cloning
- You want to avoid the overhead of initializing an object
clone keyword), making this pattern easy to implement.Structural Patterns
What's the difference between Adapter and Facade?
What's the difference between Adapter and Facade?
- Makes two incompatible interfaces work together
- Wraps an existing class with a new interface
- Like a card reader adapting your memory card to work with your computer
- Use when you need to integrate incompatible interfaces
- Provides a simplified interface to a complex subsystem
- Creates a new, simpler interface to existing complex code
- Like a computer power button hiding all the complex startup processes
- Use when you want to hide complexity
When should I use Decorator vs inheritance?
When should I use Decorator vs inheritance?
- You need to add behavior dynamically at runtime
- You want to add responsibilities to individual objects, not entire classes
- You want to avoid creating an explosion of subclasses
- You need combinations of behaviors (like coffee with milk, whip, and vanilla)
- The behavior is fundamental to the type
- All instances of the class should have this behavior
- The behavior won’t change at runtime
- You’re modeling an “is-a” relationship
What's the telescoping constructor anti-pattern that Builder solves?
What's the telescoping constructor anti-pattern that Builder solves?
- The number of constructor parameters can quickly get out of hand
- It becomes difficult to understand the arrangement of parameters
- The parameter list could keep growing as you add more options
- Easy to make mistakes with parameter order
- Provides a fluent interface for construction
- Makes the code more readable
- Allows optional parameters without constructor pollution
- Example:
new BurgerBuilder(14)->addPepperoni()->addLettuce()->build()
What's the difference between Proxy and Decorator?
What's the difference between Proxy and Decorator?
- Controls access to the object
- Often manages the lifecycle of the real object
- Examples: lazy loading, access control, logging
- The proxy represents the object
- Adds new behavior to the object
- Enhances or modifies existing behavior
- Can stack multiple decorators
- Examples: adding toppings to coffee
Behavioral Patterns
What's the difference between Strategy and State patterns?
What's the difference between Strategy and State patterns?
- Allows switching algorithms/strategies based on the situation
- The client usually chooses the strategy
- Example: Choosing between bubble sort and quick sort based on data size
- Changes behavior when the object’s state changes
- State transitions happen internally
- Example: A phone behaving differently when idle, picked up, or calling
When should I use Observer pattern?
When should I use Observer pattern?
- Multiple objects need to be notified of state changes in another object
- You want to establish a one-to-many dependency between objects
- Changes to one object require changing others, and you don’t know how many
- An object should notify others without knowing who they are
- Job seekers subscribing to job postings
- Newsletter subscriptions
- Event listeners in UI frameworks
- Pub/sub messaging systems
When do I need Chain of Responsibility?
When do I need Chain of Responsibility?
- You have multiple potential handlers for a request
- You want to give more than one object a chance to handle a request
- You want to avoid coupling the sender to the receiver
- The handlers should be chained in a specific order
What's the difference between Command and Strategy?
What's the difference between Command and Strategy?
- Encapsulates a request as an object
- Focuses on encapsulating actions/operations
- Supports undo/redo functionality
- Can queue or log operations
- Example: Remote control buttons
- Encapsulates an algorithm
- Focuses on interchangeable algorithms
- No built-in undo/redo
- Selects algorithm at runtime
- Example: Different sorting algorithms
When should I implement Iterator vs use language built-ins?
When should I implement Iterator vs use language built-ins?
- The language provides adequate iteration support
- You’re working with standard collections
- The default iteration order is what you need
- You need a special traversal algorithm
- You’re creating a custom collection
- You need multiple simultaneous iterations
- You want to hide the internal structure completely
Practical Application
How do I know which pattern to use?
How do I know which pattern to use?
- Identify the problem clearly - What are you trying to achieve?
- Consider simple solutions first - Do you even need a pattern?
- Match the problem type:
- Object creation problem? → Creational pattern
- Object composition problem? → Structural pattern
- Object interaction problem? → Behavioral pattern
- Review specific patterns in that category
- Verify the fit - Does the pattern truly solve your problem?
- Consider the trade-offs - Is the added complexity worth it?
Can I use multiple patterns together?
Can I use multiple patterns together?
- Abstract Factory can be implemented using Factory Method or Prototype
- Builder can use Composite for building complex structures
- Composite and Decorator often work together
- Facade can use Singleton for the facade instance
- Observer can use Mediator to manage complex notifications
- Strategy and Factory together for selecting and creating strategies
How do I convince my team to use design patterns?
How do I convince my team to use design patterns?
- Focus on solving problems - Let patterns emerge naturally
- Demonstrate value - Show how a pattern solves a specific pain point
- Use pattern names - They provide a shared vocabulary
- Start small - Introduce one pattern at a time
- Share knowledge - Explain why, not just how
- Lead by example - Use patterns appropriately in your code
Should I learn all 23 patterns?
Should I learn all 23 patterns?
- Factory Method
- Singleton (and why it’s often an anti-pattern)
- Adapter
- Decorator
- Observer
- Strategy
- Abstract Factory
- Builder
- Facade
- Proxy
- Command
- Template Method
- All the others
Are these patterns language-specific?
Are these patterns language-specific?
- Some patterns are built into certain languages (like Iterator in Python)
- Some patterns are easier to implement in certain languages
- Some patterns are less necessary depending on language features
- The implementation details vary by language
What's the best way to learn design patterns?
What's the best way to learn design patterns?
- Understand the problem each pattern solves
- Study real-world examples - see the pattern in context
- Implement patterns yourself - hands-on practice
- Recognize patterns in existing code - see them in the wild
- Use patterns to solve real problems - practical application
- Teach others - explaining solidifies understanding
- Just memorize implementations
- Force patterns into projects
- Learn all patterns at once
- Skip understanding the “why”
- Focus on principles and intent
- Let patterns emerge from real needs
- Learn gradually over time
- Study why patterns work