LLDbeginner

Factory Pattern

The Factory Pattern centralizes object creation and helps calling code remain independent from concrete implementations. It is a common answer when runtime type selection is involved.

Reading time

8 min

factorydesign patternsobject creationoop

What Factory Solves

If code directly constructs many concrete classes, extension becomes painful. A factory hides construction details and returns the right implementation based on input or configuration.

Good Use Cases

  • Payment providers
  • Notification channels
  • Storage adapters
  • File parsers

Benefits

  • Reduced coupling
  • Easier testing
  • Simpler extension

Interview Tip

If you notice object creation branching all over the codebase, factory is often the right abstraction.