00:00

Abstract Factory Design Pattern

What is the Abstract Factory Design Pattern?

The Abstract Factory Design Pattern is a creational design pattern. It is used when we want to create families of related objects without specifying their exact concrete classes.

In simple words, an Abstract Factory provides an interface to create multiple related objects, but the actual creation logic is handled by concrete factory classes.

Real-Life Example

Think of a furniture factory. One factory creates Modern furniture (chair and sofa), and another factory creates Victorian furniture. You don’t care about how the furniture is made — you just want matching furniture.

When Should We Use Abstract Factory?

  • When the system should be independent of how objects are created
  • When multiple families of related objects are required
  • When we want to ensure that related objects are used together

Key Components of Abstract Factory

  • Abstract Factory – Declares methods to create abstract products
  • Concrete Factory – Implements the abstract factory
  • Abstract Product – Interface for product types
  • Concrete Product – Actual implementations of products

Example: GUI Application (Button & Checkbox)

Step 1: Create Abstract Products


interface Button {
    void paint();
}

interface Checkbox {
    void paint();
}

Step 2: Create Concrete Products


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

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

class WindowsCheckbox implements Checkbox {
    public void paint() {
        System.out.println("Rendering Windows Checkbox");
    }
}

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

Step 3: Create Abstract Factory


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

Step 4: Create 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


class Application {
    private Button button;
    private Checkbox checkbox;

    Application(GUIFactory factory) {
        button = factory.createButton();
        checkbox = factory.createCheckbox();
    }

    void renderUI() {
        button.paint();
        checkbox.paint();
    }
}

public class Main {
    public static void main(String[] args) {
        GUIFactory factory = new WindowsFactory(); 
        Application app = new Application(factory);
        app.renderUI();
    }
}

Output

Rendering Windows Button
Rendering Windows Checkbox

Advantages of Abstract Factory

  • Ensures consistency among related products
  • Makes code loosely coupled
  • Easy to switch between product families

Disadvantages of Abstract Factory

  • Code becomes complex with many interfaces
  • Adding new product types requires changes in factories

Abstract Factory vs Factory Method

Abstract Factory Factory Method
Creates families of related objects Creates a single object
Uses composition Uses inheritance

Conclusion

The Abstract Factory Design Pattern is useful when your application needs to work with multiple families of related objects while keeping the code clean, flexible, and easy to maintain. It is widely used in frameworks, UI toolkits, and enterprise-level applications.