Design Patterns interview question · Most asked
Compare Factory Method, Abstract Factory and Builder.
Summary
Factory Method creates one product, with the concrete class chosen by a subclass; Abstract Factory creates a matching family of related products through one factory object; Builder creates one complex product step by step with validation at the end.
Longer Answer
Choose Factory Method (or a simple factory) when the question is "which class?"; Abstract Factory when several objects must match (theme widgets, vendor drivers); Builder when the question is "how do I assemble this complex object readably?" (many optional parts, invalid combinations).
They combine: an abstract factory can use builders internally, and a builder can be returned by a factory.
Key Points
- Factory Method: which class, via subclass.
- Abstract Factory: matching families.
- Builder: step-by-step assembly.
Deep dive: Design Patterns, Explained Simply: Choosing the Right Pattern
Explanation
What interviewers look for
The question each pattern answers, with examples.
More most-asked Design Patterns questions
- What problem does the Builder pattern solve? Show an example.
- Explain the SOLID principles.
- What is a design pattern, and why are they useful?
- What is the difference between dependency inversion, dependency injection and inversion of control?
- Why is composition often preferred over inheritance?
- What is dependency injection, and what forms does it take?
- Explain the Factory Method pattern and how it differs from a simple factory.
- How do you implement a Singleton, and why is it often considered an anti-pattern?