Design Pattern

Abstract Factory Pattern

Clean Java-only production-ready implementation.


What Problem It Solves

When you have FAMILIES of related objects that must be used together (e.g., "dark theme" = DarkButton + DarkPanel + DarkText). You don't want DarkButton mixed with LightPanel. Abstract Factory lets you create entire families through one interface.

Abstract Factory vs Factory Method

AspectFactory MethodAbstract Factory
CreatesOne product typeA family of products
InterfaceSingle create() methodMultiple creation methods
ExtensionNew subclass for new productNew factory for new family
Use in LLDParking spot, chess pieceUI toolkit, cross-platform widgets

When Simple Factory Isn't Enough

Simple Factory creates one product. Abstract Factory creates a PRODUCT FAMILY (multiple related products).

Where This Appears in LLD

Rare in typical machine coding. Use it when a problem explicitly asks for "themes," "skins," "cross-platform support," or "multiple rendering styles."

Implementation

// โ”€โ”€โ”€ EXAMPLE 1 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
// WHAT WE ARE IMPLEMENTING:
// A cross-platform UI theme manager supporting Dark and Light skins
// consistently.
//
// WHERE THE PATTERN FITS IN:
// ThemeFactory acts as the Abstract Factory. DarkThemeFactory and
// LightThemeFactory are Concrete Factories creating matching Button and
// Checkbox products.
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
// --- Product interfaces ---
interface Button { void render(); }
interface Checkbox { void render(); }
interface ThemeFactory {
    Button createButton();
    Checkbox createCheckbox();
}

// --- Dark theme family ---
class DarkButton implements Button {
    public void render() { System.out.println("  [DarkButton] Rendered with dark bg"); }
}

class DarkCheckbox implements Checkbox {
    public void render() { System.out.println("  [DarkCheckbox] Rendered dark"); }
}

class DarkThemeFactory implements ThemeFactory {
    public Button createButton() { return new DarkButton(); }
    public Checkbox createCheckbox() { return new DarkCheckbox(); }
}

// --- Light theme family ---
class LightButton implements Button {
    public void render() { System.out.println("  [LightButton] Rendered with light bg"); }
}

class LightCheckbox implements Checkbox {
    public void render() { System.out.println("  [LightCheckbox] Rendered light"); }
}

class LightThemeFactory implements ThemeFactory {
    public Button createButton() { return new LightButton(); }
    public Checkbox createCheckbox() { return new LightCheckbox(); }
}

class Application {
    private final Button button;
    private final Checkbox checkbox;
    public Application(ThemeFactory factory) {
        button = factory.createButton();
        checkbox = factory.createCheckbox();
    }
    void render() {
        button.render();
        checkbox.render();
    }
}

public class Main {
    public static void main(String[] args) {
        // Choose family once โ€” all products are consistent
        Application app = new Application(new DarkThemeFactory());
        app.render();
    }
}

๐Ÿ’ฌReview

Help Us Improve

How helpful was this walkthrough?

Click a star to rate. We actively use this feedback to refine and update our system design content.

Placeholder
Optional but highly appreciated!

Discussion

Share your thoughts, ask questions, or help others.

Loading comments...