Course contentsShow
← Software Engineering
Lesson 242 of 1,9337. Creational Design PatternsFree lesson

Factory Pattern Trade-offs

Evaluate when factories add value versus when they introduce unnecessary indirection and complexity.

Factory Pattern Trade-offs

You'll learn when factories help your code and when they just get in the way.

The Core Idea

Not every object creation needs a factory. While factories can provide flexibility and centralize object creation logic, they also add extra classes, indirection, and complexity. The key is recognizing when the benefits outweigh the costs.

When Factories Add Value

Factories shine when:

  • Object creation is complex – Multiple setup steps, configuration, or conditional logic make instantiation messy
  • Creation logic changes frequently – The "how" of building objects evolves, and you want to isolate those changes
  • You need runtime flexibility – Which concrete class to instantiate depends on configuration, user input, or other dynamic factors
  • You're hiding dependencies – Clients shouldn't know about all the concrete classes that might be created

Think of a restaurant menu: if you're always ordering the exact same simple sandwich, having a "sandwich factory" that asks 15 questions is overkill. But if you run a catering business creating custom meals based on dietary restrictions, guest counts, and themes, that factory becomes invaluable.

When Factories Introduce Unnecessary Complexity

Skip the factory when:

  • Creation is trivial – A simple constructor call with no logic is clear and direct
  • You have only one concrete class – No variation means no need for abstraction
  • The abstraction is premature – You're guessing at future flexibility you might never need (YAGNI)
  • Testing becomes harder – Adding layers makes mocking and testing more cumbersome without corresponding benefits

Key Takeaway: Factories are powerful when object creation involves complexity, variation, or change—but they're overhead when instantiation is simple and stable. Choose them deliberately, not automatically.