Can you describe a real-world scenario where you would use the Abstract Factory pattern?

The Abstract Factory pattern fits scenarios where you need to create families of related objects that must stay consistent with each other, without the client knowing which concrete family it's working with. A cross-platform UI toolkit is the classic real-world case.

Key Points

Example

A cross-platform UI library uses a WindowsFactory to build a WindowsButton and WindowsCheckbox, and a MacFactory to build matching Mac-styled components, so the application code never mixes styles from different platforms.

public class CrossPlatformUI {

    interface Button { void click(); }
    interface Checkbox { void toggle(); }
    interface GUIFactory { Button createButton(); Checkbox createCheckbox(); }

    static class WindowsFactory implements GUIFactory {
        public Button createButton() { return () -> System.out.println("Windows-style button clicked"); }
        public Checkbox createCheckbox() { return () -> System.out.println("Windows-style checkbox toggled"); }
    }

    static class MacFactory implements GUIFactory {
        public Button createButton() { return () -> System.out.println("Mac-style button clicked"); }
        public Checkbox createCheckbox() { return () -> System.out.println("Mac-style checkbox toggled"); }
    }

    // Application code only knows the interfaces
    static class Application {
        private final Button button;
        private final Checkbox checkbox;
        Application(GUIFactory factory) {
            button = factory.createButton();
            checkbox = factory.createCheckbox();
        }
        void run() { button.click(); checkbox.toggle(); }
    }

    public static void main(String[] args) {
        String os = "mac";   // normally System.getProperty("os.name")
        GUIFactory factory = os.contains("win") ? new WindowsFactory() : new MacFactory();
        new Application(factory).run();
    }
}

Output

Mac-style button clicked

Mac-style checkbox toggled

Interview Tip

A concise interview answer is

A classic example is a UI toolkit that has to render consistently on Windows, Mac, and Linux — each OS gets its own concrete factory that produces a whole matching family of buttons, checkboxes, and text fields. The application code only ever talks to the abstract factory and abstract product interfaces, so it never risks mixing styles from different platforms.

Last updated: 1 November 2026

Previous question: Can you provide an example where the Factory pattern would simplify object creation?

Next question: How would you implement the Abstract Factory pattern in Java?

More Java Design Patterns interview questions

All Java Design Patterns questions