Abstract Factory — factory of factories
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Abstract Factory is a pattern for creating families of related objects that must work together and stay consistent.
The classic example is a UI toolkit. Imagine an app that runs on Windows and macOS. You need buttons, text fields, and scroll bars, but the Windows versions look different from the macOS versions.
If developers can mix and match — a Windows button with a macOS text field — the interface looks broken. Abstract Factory prevents that. You define a GUIFactory interface with methods like createButton, createTextField, and createScrollBar.
Then you implement two concrete factories, WindowsGUIFactory and MacGUIFactory, and each returns the matching set. The client code depends only on the GUIFactory interface, so you can switch the entire family by changing one line — which factory you instantiate. If the user is on Windows, you use WindowsGUIFactory and everything is Windows style; on macOS you swap to MacGUIFactory and everything matches.
The key difference from Factory Method is that Factory Method creates one product, while Abstract Factory creates several related products. A backend example is JDBC: a driver creates a matching Connection, Statement, and ResultSet, all from the same vendor family, so swapping the driver gives you a consistent set for the new database. The mistake freshers make is confusing Abstract Factory with Factory Method.
The giveaway is the word family — if you're creating multiple related objects that belong together and must stay consistent, it's Abstract Factory. Another mistake is grouping unrelated objects into one factory just because they're created in the same place. Use Abstract Factory only when consistency across the family actually matters.
Abstract Factory — factory of factories
Direct Answer
Abstract Factory provides an interface for creating a related set of product types without exposing their concrete classes. A concrete factory supplies one variant of the family, such as Windows buttons and dialogs. It encourages compatible products to be selected together, but consistency still depends on the factory implementation and on not mixing products from other creation paths.
Common Mistakes & Pitfalls
- Confusing it with Factory Method. The tell is 'family' — multiple related products that must match means Abstract Factory.
- Grouping unrelated objects. Don't put
createUserRepositoryandcreateEmailValidatorin one factory just because they're nearby — they aren't a family. - Missing the consistency guarantee. The whole point is preventing mix-and-match across families.
- Using it for a single product. That's Factory Method or Simple Factory.
Check your understanding
Answer these without looking back at the article.
- What is the difference between Abstract Factory and Factory Method?
- Where is Abstract Factory used in the Java standard library?
- How does Abstract Factory enforce consistency across a product family?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.