100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the Abstract Factory Pattern?

The Abstract Factory pattern explained — creating compatible families of related objects, with a Java UI toolkit example and interview Q&A.

mediumQ166 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes, guaranteeing that the objects produced by one concrete factory are designed to work together consistently.

A client depends only on the abstract factory interface and the abstract product interfaces, never on concrete classes, which means the entire family of concrete implementations can be swapped by substituting one concrete factory for another. This is common in cross-platform UI toolkits (a WindowsFactory versus a MacFactory each producing matching buttons, checkboxes, and scrollbars) and in database or cloud-provider abstractions where switching an entire backend family should require changing only one factory instantiation. Internally, each creation method on the factory interface behaves like its own Factory Method, and the pattern’s real payoff is preventing accidental mixing of incompatible components from different families. The trade-off is rigidity: adding a brand-new product type to the family requires changing the abstract factory interface and every concrete factory that implements it.

  • Guarantees created products from one family are compatible with each other
  • Isolates client code from concrete classes entirely
  • Makes swapping an entire product family a one-line change
  • Enforces consistency across related objects at compile time

AI Mentor Explanation

An official kit supplier for a national team must produce an entire matching family in one call — jersey, cap, and equipment bag, all in the correct team colors and sponsor branding. A club never orders a jersey from one supplier and a cap from an unrelated one, because the whole point is a guaranteed matching set. That is Abstract Factory: one factory interface produces an entire compatible family of related objects, so the client never has to worry about mismatched pieces.

Step-by-Step Explanation

  1. Step 1

    Define abstract product interfaces

    Declare an interface for each product type in the family, e.g. Button and Checkbox.

  2. Step 2

    Define the abstract factory interface

    Declare a creation method for each product type, e.g. createButton() and createCheckbox().

  3. Step 3

    Implement one concrete factory per family

    Each concrete factory (e.g. DarkThemeFactory) implements every method, returning products from that family only.

  4. Step 4

    Client depends only on the abstractions

    Client code receives one UIFactory instance and calls its methods, never referencing concrete classes directly.

What Interviewer Expects

  • A precise definition mentioning “family of related objects” and “without specifying concrete classes”
  • A real-world example such as cross-platform UI toolkits or swappable database backends
  • Awareness of the trade-off: adding a new product type requires changing every concrete factory
  • Recognition that each creation method is itself effectively a Factory Method

Common Mistakes

  • Confusing Abstract Factory with a single Factory Method producing one product
  • Forgetting the core guarantee is family-level consistency, not just object creation
  • Not mentioning the difficulty of extending the family with a new product type
  • Describing it as merely “a factory of factories” without the compatibility guarantee

Best Answer (HR Friendly)

Abstract Factory gives you one interface for creating a whole set of related objects, like a matching family of UI components, without the client ever touching concrete classes. You swap the entire family by swapping which concrete factory you use, so a Windows-style set of controls and a Mac-style set of controls never accidentally get mixed together. The trade-off is that adding a brand-new type of product to the family means updating every concrete factory that implements the interface.

Code Example

Abstract Factory producing a consistent UI family
interface Button { void render(); }
interface Checkbox { void render(); }

interface UIFactory {
    Button createButton();
    Checkbox createCheckbox();
}

// Family 1: Light theme
class LightButton implements Button {
    public void render() { System.out.println("Light button"); }
}
class LightCheckbox implements Checkbox {
    public void render() { System.out.println("Light checkbox"); }
}
class LightThemeFactory implements UIFactory {
    public Button createButton() { return new LightButton(); }
    public Checkbox createCheckbox() { return new LightCheckbox(); }
}

// Family 2: Dark theme
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(); }
}

class Application {
    private final UIFactory factory;
    Application(UIFactory factory) { this.factory = factory; }
    void render() {
        factory.createButton().render();
        factory.createCheckbox().render();
    }
}

// swapping the entire family is a one-line change
new Application(new DarkThemeFactory()).render();

Follow-up Questions

  • How does Abstract Factory differ from Factory Method?
  • What happens to existing concrete factories when you add a new product type to the family?
  • Where have you seen Abstract Factory used in a real framework or library?
  • How does Abstract Factory support the open/closed principle when adding a new family?

MCQ Practice

1. What does the Abstract Factory pattern primarily guarantee?

The core value of Abstract Factory is producing a family of products designed to work together consistently.

2. A classic real-world use case for Abstract Factory is?

Producing a consistent family of platform-specific widgets (buttons, checkboxes) is the textbook Abstract Factory example.

3. A key trade-off of Abstract Factory is?

Extending the family with a new product type forces changes across the abstract interface and all its implementations.

Flash Cards

Abstract Factory in one line?An interface for creating a whole family of related objects without specifying concrete classes.

Core guarantee?Products from one concrete factory are compatible with each other.

Classic example?Cross-platform UI toolkits producing matching Windows-style or Mac-style widget sets.

Main trade-off?Adding a new product type requires updating the interface and every concrete factory implementing it.

1 / 4

Continue Learning