Factory Method vs Abstract Factory
Factory Method vs Abstract Factory pattern compared — one product via subclassing vs a compatible product family — with Java examples.
Expected Interview Answer
Factory Method defines a single method, usually overridden by subclasses, that creates one product, while Abstract Factory defines an interface with multiple factory methods for creating a whole family of related products meant to be used together.
Factory Method relies on inheritance: a creator class declares a method that subclasses override to produce a specific product type, and the pattern is often used inside a template method to keep the overall algorithm fixed while letting subclasses vary the object created. Abstract Factory relies on composition: it defines an interface with several creation methods (createButton(), createCheckbox()) and concrete factories implement all of them to produce a consistent family of objects, such as an entire set of Windows-style or macOS-style UI widgets that must not be mixed. A useful rule of thumb: Factory Method is one method producing one kind of product, typically via subclassing; Abstract Factory is an object producing several related kinds of products, typically via composition, and it is often implemented internally using multiple Factory Methods.
- Factory Method lets subclasses decide which concrete product to instantiate
- Abstract Factory guarantees a family of products are mutually compatible
- Both decouple client code from concrete class instantiation
- Both follow the open/closed principle by adding new factories, not editing existing code
AI Mentor Explanation
Factory Method is like a regional academy where each branch overrides one “produceBowler()” method to train the type of bowler suited to its local pitch conditions — one method, one product type, varied by subclass. Abstract Factory is like a full national team-kit supplier who must produce a whole matching set — jersey, cap, and bat sponsor branding — that are all guaranteed to be consistent with each other; you don’t mix a red-team jersey with a blue-team cap. One overrides a single creation step; the other guarantees an entire compatible family.
Step-by-Step Explanation
Step 1
Count the products
If you need to create one kind of product with variation, consider Factory Method; if you need a whole family of related products, consider Abstract Factory.
Step 2
Factory Method: define an overridable creation method
A creator base class declares createProduct(); subclasses override it to return a specific concrete product.
Step 3
Abstract Factory: define a factory interface with multiple methods
One interface exposes several creation methods, and each concrete factory implements all of them for one consistent product family.
Step 4
Verify consistency guarantees
Abstract Factory should make it impossible to accidentally mix products from different families; Factory Method has no such family-level guarantee.
What Interviewer Expects
- Correct scope distinction: one product vs a family of products
- Mention that Factory Method commonly uses inheritance, Abstract Factory commonly uses composition
- Awareness that Abstract Factory is often implemented using several internal Factory Methods
- A concrete example such as UI toolkit families for Abstract Factory
Common Mistakes
- Saying the two patterns are interchangeable in every situation
- Forgetting that Abstract Factory’s primary guarantee is family consistency, not just object creation
- Claiming Factory Method always requires composition rather than subclassing
- Not recognizing that Abstract Factory interfaces are usually implemented with multiple Factory Methods internally
Best Answer (HR Friendly)
“Factory Method is a single method that subclasses override to decide which concrete object to create — it’s about varying one product. Abstract Factory is a whole interface for creating a family of related objects that must work together, like a full set of UI components matching one theme. Factory Method typically uses inheritance to vary one product; Abstract Factory typically uses composition to guarantee a whole consistent set.”
Code Example
abstract class DocumentCreator {
abstract Document createDocument();
void export() {
Document doc = createDocument();
doc.save();
}
}
interface Document { void save(); }
class PdfDocument implements Document {
public void save() { System.out.println("Saving PDF"); }
}
class PdfCreator extends DocumentCreator {
Document createDocument() { return new PdfDocument(); }
}interface Button { void render(); }
interface Checkbox { void render(); }
interface UIFactory {
Button createButton();
Checkbox createCheckbox();
}
class DarkButton implements Button {
public void render() { System.out.println("Dark button"); }
}
class DarkCheckbox implements Checkbox {
public void render() { System.out.println("Dark checkbox"); }
}
class DarkThemeFactory implements UIFactory {
public Button createButton() { return new DarkButton(); }
public Checkbox createCheckbox() { return new DarkCheckbox(); }
}
// consistent family: always Dark widgets together, never mixed with a Light one
UIFactory factory = new DarkThemeFactory();
factory.createButton().render();Follow-up Questions
- Can Abstract Factory be implemented without using Factory Method internally?
- How would you add a new product family without modifying existing factories?
- What problem does Abstract Factory solve that a simple if/else creation block does not?
- When would Factory Method alone be sufficient instead of Abstract Factory?
MCQ Practice
1. Factory Method typically relies on which OOP mechanism to vary the created product?
Factory Method uses subclasses overriding a creation method to decide the concrete product.
2. The main guarantee Abstract Factory provides is?
Abstract Factory ensures all created products in a family are designed to work together consistently.
3. Abstract Factory is often implemented internally using?
Each creation method on an Abstract Factory interface is itself typically a Factory Method for that product.
Flash Cards
Factory Method in one line? — A single overridable method that lets subclasses decide which concrete product to create.
Abstract Factory in one line? — An interface of multiple creation methods producing a consistent family of related products.
Which uses inheritance more directly? — Factory Method, via subclass overriding.
Which guarantees family consistency? — Abstract Factory — its products are designed to work together.