The Abstract Factory Pattern: Families of Objects
Master the Abstract Factory Pattern in Java. Learn how to create families of related objects without specifying their concrete classes.
Moshiour Rahman
Advertisement
The Problem: Inconsistent Families
Imagine you are building a UI framework. You support Light Mode and Dark Mode.
- Light Mode: White Buttons, Black Text.
- Dark Mode: Black Buttons, White Text.
You need to ensure that a DarkButton is never used with LightText. They must be created as a Family.
If you use new LightButton() everywhere, your code is messy. Mixing them up leads to ugly UIs.
The Solution: The Abstract Factory
The Abstract Factory Pattern produces families of related objects. It’s a “Factory of Factories”.
Real-Life Analogy: A Furniture Factory 🪑
- Victorian Factory: Makes
VictorianChairandVictorianSofa. - Modern Factory: Makes
ModernChairandModernSofa.
When you order from the “Modern Factory”, you are guaranteed that the Chair and Sofa will match.
Visualizing the Pattern

Implementation
1. Abstract Products
interface Button { void paint(); }
interface Checkbox { void paint(); }
2. Concrete Products
// Windows Family
class WinButton implements Button {
public void paint() { System.out.println("WinButton"); }
}
class WinCheckbox implements Checkbox {
public void paint() { System.out.println("WinCheckbox"); }
}
// Mac Family
class MacButton implements Button {
public void paint() { System.out.println("MacButton"); }
}
class MacCheckbox implements Checkbox {
public void paint() { System.out.println("MacCheckbox"); }
}
3. The Abstract Factory Interface
public interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}
4. Concrete Factories
public class WinFactory implements GUIFactory {
public Button createButton() { return new WinButton(); }
public Checkbox createCheckbox() { return new WinCheckbox(); }
}
public class MacFactory implements GUIFactory {
public Button createButton() { return new MacButton(); }
public Checkbox createCheckbox() { return new MacCheckbox(); }
}
Usage
public class Application {
private Button button;
private Checkbox checkbox;
public Application(GUIFactory factory) {
// We don't know if it's Windows or Mac, but we know they match!
button = factory.createButton();
checkbox = factory.createCheckbox();
}
public void paint() {
button.paint();
checkbox.paint();
}
public static void main(String[] args) {
GUIFactory factory;
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
factory = new WinFactory();
} else {
factory = new MacFactory();
}
Application app = new Application(factory);
app.paint();
}
}
In The Wild (Real World Examples)
1. JDBC Connection
When you load a driver (Connection, Statement, ResultSet), they are a family. You can’t use a MySQL Connection with an Oracle Statement. The DriverManager acts as a coordinator for these families.
2. Document Builders (XML Parsing)
DocumentBuilderFactory.newInstance() returns a factory that produces parsers (DocumentBuilder) compatible with the specific implementation/configuration required.
Cheat Sheet
| Feature | Details |
|---|---|
| Category | Creational |
| Problem Solved | Creating families of related objects (Theme bundles) |
| Key implementation | Interface dealing with multiple products (createA(), createB()) |
| Pros | Consistency (Products always match) |
| Cons | Complexity (Lots of classes), Rigid (Adding a new product type requires changing ALL factories) |
Tips to Remember 🧠
- Factory Method vs Abstract Factory:
- Factory Method: Produces one product.
- Abstract Factory: Produces a family of products.
Advertisement
Moshiour Rahman
Software Architect & AI Engineer
Enterprise software architect with deep expertise in financial systems, distributed architecture, and AI-powered applications. Building large-scale systems at Fortune 500 companies. Specializing in LLM orchestration, multi-agent systems, and cloud-native solutions. I share battle-tested patterns from real enterprise projects.
Related Articles
The Factory Method Pattern: Decoupling Object Creation
Master the Factory Method Pattern in Java. Learn how to loosen coupling in your code by letting subclasses decide which objects to instantiate.
JavaThe Visitor Design Pattern: Add Operations Without Modifying Classes
Master the Visitor Pattern in Java. Learn how to add new operations to object structures using double dispatch.
JavaThe Template Method Pattern: The Recipe for Success
Master the Template Method Pattern in Java. Learn how to define the skeleton of an algorithm in a superclass but let subclasses override specific steps.
Comments
Comments are powered by GitHub Discussions.
Configure Giscus at giscus.app to enable comments.