About Us Contact Us Write for Us Advertise
Home > System Design > Low-Level Design > Abstract Factory Pattern in Java (with Example)
System Design › Low-Level Design

Abstract Factory Pattern in Java (with Example)

The Abstract Factory pattern explained: a factory of factories that creates whole families of related objects (a matching button + checkbox) consistently. Java code, UML, and Factory Method vs Abstract Factory.

Shiv Pandey
Shiv Pandey
Oct 18, 2026 | 89 views
Abstract Factory Pattern in Java (with Example)

🟠 Design Patterns · Creational · Senior

The Abstract Factory takes the Factory Method one level up. Where a factory method makes one product, an abstract factory makes a whole family of related products that are meant to work together — and guarantees you never accidentally mix families. The classic example is cross-platform UI: a Windows look-and-feel vs a Mac one, each with its own matching set of widgets.

In one line: Abstract Factory is a "factory of factories" — an interface for creating a family of related objects (a button + a checkbox + a scrollbar) without specifying their concrete classes, so a whole consistent set is chosen at once.

The problem

Imagine a UI toolkit that must render on Windows and Mac. Each platform needs a Button, a Checkbox, a Menu — and you must never put a MacButton next to a WindowsCheckbox. Using a plain factory per widget, nothing stops that mismatch. You need a way to pick an entire consistent family in one decision.

The solution

🟠 Define an abstract factory interface with a create-method for each product in the family, then one concrete factory per family:

interface GUIFactory {          // the abstract factory
    Button   createButton();
    Checkbox createCheckbox();
}
class WindowsFactory implements GUIFactory {
    public Button   createButton()   { return new WindowsButton(); }
    public Checkbox createCheckbox() { return new WindowsCheckbox(); }
}
class MacFactory implements GUIFactory {
    public Button   createButton()   { return new MacButton(); }
    public Checkbox createCheckbox() { return new MacCheckbox(); }
}
// client picks ONE factory, gets a matching family:
GUIFactory f = onWindows ? new WindowsFactory() : new MacFactory();
Button b   = f.createButton();     // guaranteed to match…
Checkbox c = f.createCheckbox();   // …this checkbox
GUIFactory WindowsFactory MacFactory WindowsButton WindowsCheckbox MacButton MacCheckbox

The client picks one factory and every product it hands out belongs to the same family — a Mac factory can never produce a Windows button. Consistency is guaranteed by construction.

Abstract Factory vs Factory Method

🟠 The distinction interviewers probe:

Factory Method Abstract Factory
Creates one product Creates a family of related products
One create method Several create methods (one per product)
"Which subclass of X?" "Which whole set — Windows or Mac?"

When to use it

🟠 Use Abstract Factory when your system needs to work with multiple families of related objects and you must keep a family consistent: cross-platform UI, database drivers for different vendors (each providing a matching Connection, Command, Reader), or theme systems (dark vs light producing matching components). If you only have one product type, that's just Factory Method — don't over-build.

The interview-ready summary

"Abstract Factory is a factory of factories: an interface with a create-method per product, and one concrete factory per family. The client picks one factory and gets a fully consistent set of related objects — you can't mix a Mac button with a Windows checkbox. It's Factory Method scaled to families. I'd use it for cross-platform UI, per-vendor DB drivers, or themes — anywhere a group of objects must vary together and stay compatible." That draws the Factory-Method contrast cleanly, which is what they're checking."

What to read next

← Factory Method pattern · Builder pattern →

Related Articles

Factory Method Pattern in Java (with Example)
System Design › Low-Level Design

Factory Method Pattern in Java (with Example)

Chain of Responsibility Pattern in Java (Example)
System Design › Low-Level Design

Chain of Responsibility Pattern in Java (Example)

Template Method Pattern in Java (with Example)
System Design › Low-Level Design

Template Method Pattern in Java (with Example)

Command Pattern in Java (with Undo Example)
System Design › Low-Level Design

Command Pattern in Java (with Undo Example)