Factory Method vs Abstract Factory — When to Use Each
Fast recall
Quick revision
Core answer
Ability to distinguish creational patterns and select the right level of abstraction
Treating them as interchangeable or thinking Abstract Factory is just a 'bigger' Factory Method
Give a real-world example like UI toolkit families (Button/Checkbox per OS) for Abstract Factory, and Spring's FactoryBean for Factory Method
Interview-ready guide
Interview answer
The core distinction is scale. Factory Method handles one product type with multiple variants — a base class defines a createNotification() method, and subclasses override it to return an EmailNotification or PushNotification. The mechanism is inheritance: the subclass controls instantiation, the base class owns the algorithm.
Abstract Factory handles families of related products. A PaymentFactory interface declares createGateway(), createWebhookHandler(), and createRefundProcessor(). A StripeFactory implements all three, guaranteeing every product is Stripe-compatible. The mechanism is composition — the client holds a factory reference and delegates to it.
The critical value of Abstract Factory is consistency enforcement. Without it, nothing prevents a developer from wiring a StripeGateway with a PayPalWebhookHandler, which would fail at runtime. The factory ensures all products in the family come from the same source.
My practical rule: start with Factory Method when you have one varying product. Promote to Abstract Factory when two conditions appear — multiple related products that vary together, and incompatible combinations that need prevention. In Spring, FactoryBean is Factory Method — override getObject(). The ApplicationContext itself is a large Abstract Factory, producing beans, resources, and events as a coherent family. Don't reach for Abstract Factory if you have one product and two variants — that's over-engineering a solved problem.
Factory Method vs Abstract Factory — When to Use Each
Direct Answer
Factory Method lets a subclass decide which object to create (single product, one abstraction level). Abstract Factory creates families of related objects without specifying their concrete classes (multiple related products). Use Factory Method for one product hierarchy; use Abstract Factory when you need consistent families of products.
Common Mistakes & Pitfalls
Treating them as interchangeable or thinking Abstract Factory is just a 'bigger' Factory Method
Check your understanding
Answer these without looking back at the article.
- How does Spring's FactoryBean implement the Factory Method pattern?
- Can you combine Factory Method and Abstract Factory?
- How would you add a new product to an Abstract Factory without breaking existing code?
Related interview prep categories
2Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.