Java Zero to All All questionsPractise free
Interview questions › Design Patterns in Java › Creational Design Patterns

Compare Factory Method, Abstract Factory, and Builder — when would you pick each?

Level III · MidCreational Design Patterns
Answer

Factory Method varies one product via inheritance (subclasses override a creation step), Abstract Factory varies a whole family of related products via composition with a consistency guarantee, and Builder isn't about polymorphic choice at all — it's staged construction of one complex, often immutable object with many optional fields. So: pick Factory Method when a base class can't know the concrete subtype but subclasses can; Abstract Factory when products must match (a MySQL dialect with a MySQL pager); Builder when you'd otherwise have a telescoping constructor. In practice DI subsumes most factory needs, leaving Builder as the one you still hand-write frequently.

Common mistakes
  • Mixing up Factory Method and Abstract Factory.
  • Using a Builder for objects with two fields.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Creational Design Patterns.

Practise free

More on Creational Design Patterns