Bird
Raised Fist0

Which design pattern or approach best handles this scenario?

hard🎤 Interviewer Follow-up Q10 of Q15
OOP & Design Patterns - Factory vs Abstract Factory vs Builder - When to Use Each
Suppose you need to build a complex object with optional parts, but the parts depend on runtime conditions and can be added or removed dynamically after construction. Which design pattern or approach best handles this scenario?
AComposite Pattern combined with Builder for dynamic part management
BAbstract Factory Pattern with fixed product families
CBuilder Pattern with fixed construction steps
DFactory Pattern with multiple subclasses
Step-by-Step Solution
Solution:
  1. Step 1: Analyze dynamic part addition/removal need

    Builder alone assumes fixed construction steps; it does not support dynamic modification post-construction.
  2. Step 2: Identify pattern supporting dynamic hierarchical parts

    Composite pattern allows dynamic tree-like part management with add/remove operations.
  3. Step 3: Combine Builder and Composite

    Builder can construct initial object; Composite manages dynamic parts thereafter.
  4. Final Answer:

    Option A → Option A
  5. Quick Check:

    Composite supports dynamic part changes, Builder handles construction [OK]
Quick Trick: Dynamic parts -> Composite pattern needed beyond Builder [OK]
Common Mistakes:
MISTAKES
  • Assuming Builder supports dynamic changes
  • Using Factory for dynamic parts
Trap Explanation:
PITFALL
  • Candidates often think Builder alone can handle dynamic part changes.
Interviewer Note:
CONTEXT
  • Tests readiness for complex real-world pattern combinations.
Master "Factory vs Abstract Factory vs Builder - When to Use Each" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes