Factory Pattern vs 🏭 Abstract Factory Pattern in Java

Abstract Factory Pattern Example in Java

From: AI Tools/OpenAI/Chatgpt

Perfect 👍 Let me give you a clean, copyright-free Abstract Factory Pattern Java example with explanation.


🏭 Abstract Factory Pattern Example in Java

Problem:

We want to create UI components (Button, Checkbox) for different operating systems (Windows and Mac). The client should be able to request them without worrying about which OS family is used.


Step 1: Define Product Interfaces

// Product A
interface Button {
    void paint();
}

// Product B
interface Checkbox {
    void check();
}

Step 2: Create Concrete Products

// Windows Variants
class WindowsButton implements Button {
    public void paint() {
        System.out.println("Rendering a Windows style Button");
    }
}

class WindowsCheckbox implements Checkbox {
    public void check() {
        System.out.println("Checking a Windows style Checkbox");
    }
}

// Mac Variants
class MacButton implements Button {
    public void paint() {
        System.out.println("Rendering a Mac style Button");
    }
}

class MacCheckbox implements Checkbox {
    public void check() {
        System.out.println("Checking a Mac style Checkbox");
    }
}

Step 3: Define Abstract Factory

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

Step 4: Concrete Factories

class WindowsFactory implements GUIFactory {
    public Button createButton() {
        return new WindowsButton();
    }
    public Checkbox createCheckbox() {
        return new WindowsCheckbox();
    }
}

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

Step 5: Client Code

public class AbstractFactoryDemo {
    public static void main(String[] args) {
        // Choose factory based on OS
        GUIFactory factory;

        String os = "Windows"; // could be detected dynamically

        if (os.equals("Windows")) {
            factory = new WindowsFactory();
        } else {
            factory = new MacFactory();
        }

        // Use factory to create related objects
        Button button = factory.createButton();
        Checkbox checkbox = factory.createCheckbox();

        // Work with products
        button.paint();
        checkbox.check();
    }
}

✅ Output (if OS = Windows)

Rendering a Windows style Button
Checking a Windows style Checkbox

✅ Output (if OS = Mac)

Rendering a Mac style Button
Checking a Mac style Checkbox

🔑 Key Points:

  • Abstract Factory (GUIFactory) ensures we can switch between families of products easily.
  • The client never directly instantiates WindowsButton or MacButton; it just asks the factory.
  • This makes the code flexible, scalable, and easier to maintain.

👉 Would you like me to also prepare a simpler “real-life” Abstract Factory example (like 🍕 fast-food meal combos) so you can post something more fun and less technical on your blog?

Leave a Reply