interface Button {
    void render();
}

interface Checkbox {
    void render();
}

class WinButton implements Button {
    public void render() {
        System.out.println("Windows Button");
    }
}

class MacButton implements Button {
    public void render() {
        System.out.println("Mac Button");
    }
}

class WinCheckbox implements Checkbox {
    public void render() {
        System.out.println("Windows Checkbox");
    }
}

class MacCheckbox implements Checkbox {
    public void render() {
        System.out.println("Mac Checkbox");
    }
}

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

class WinFactory implements GUIFactory {
    public Button createButton() {
        return new WinButton();
    }
    public Checkbox createCheckbox() {
        return new WinCheckbox();
    }
}

class MacFactory implements GUIFactory {
    public Button createButton() {
        return new MacButton();
    }
    public Checkbox createCheckbox() {
        return new MacCheckbox();
    }
}

class Application {
    private GUIFactory factory;

    Application(GUIFactory factory) {
        this.factory = factory;
    }

    void render() {
        factory.createButton().render();
        factory.createCheckbox().render();
    }
}

public static void main(String[] args) {
    GUIFactory factory = new WinFactory();
    new Application(factory).render();
}
Back to Design Patterns
Creational Design Pattern

Abstract Factory

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It is useful when a system needs to be independent of how its products are created, composed, or represented. The pattern is commonly used in UI toolkits where different themes or operating systems require consistent sets of widgets, ensuring that products from the same family are used together.