Signing you in…

Abstract Factory

Abstract Factory is a creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. Think of it as a factory of factories — you pick a factory, and all objects you create through it are guaranteed to be compatible with each other.

ℹ️Key distinction from Factory Method: Factory Method creates one product; Abstract Factory creates an entire family of related products. All products from one factory are designed to work together.
The Problem: UI Families

You're building a cross-platform UI toolkit. You have Button and Checkbox widgets. Each platform (Windows, macOS, Linux) needs its own look-and-feel. You need to guarantee that if you create a WindowsButton, you also create a WindowsCheckbox — never a mixed family like WindowsButton + MacCheckbox.

Click to see the product family connections ▶
GUIFactory
🪟WindowsFactory
🍎MacFactory
🔲WinButton
☑️WinCheckbox
🔘MacButton
MacCheckbox
Participants
Click each participant to learn its role
🏗️
Abstract Factory
🏭
Concrete Factory
📐
Abstract Product
🎨
Concrete Product
Full Code Example
java
interface Button {
    void render();
    void onClick();
}

interface Checkbox {
    void render();
    void toggle();
}
Abstract Factory vs Factory Method
Click to compare the two patterns
🔧
Factory Method
🏗️
Abstract Factory
Abstract Factory in Java Standard Library
Click highlighted lines to see the pattern
java
1
// javax.xml.parsers.DocumentBuilderFactory
2
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
3
DocumentBuilder builder = factory.newDocumentBuilder();
4
5
// javax.xml.transform.TransformerFactory — same pattern
6
TransformerFactory tf = TransformerFactory.newInstance();
7
Transformer transformer = tf.newTransformer();
Key takeaway: Abstract Factory guarantees product compatibility. If you need to ensure that created objects work as a consistent set — buttons match checkboxes match scrollbars — Abstract Factory is the right tool.
⚠️Trade-off: Adding a new product type to the family (e.g. a Tooltip) requires changing the Abstract Factory interface and ALL concrete factory implementations. Plan your product families carefully upfront.