Advanced Java September 20 ,2025

Abstract Factory Design Pattern in Java

Introduction

The Abstract Factory Pattern is another powerful creational design pattern. While the Factory Method Pattern focuses on creating one product at a time, the Abstract Factory Pattern is about creating families of related objects without specifying their concrete classes.

In simpler words:

  • A Factory Method creates one product.
  • An Abstract Factory creates a set of products that are meant to be used together.

This pattern is especially useful when your system needs to be platform-independent or when it must support multiple product variants.

Problem Statement

Imagine you are designing a Cross-Platform UI Library that must support two environments:

  1. Windows
  2. Mac

Each environment has its own UI look and feel. For example:

  • A Windows application has Windows-style Buttons and Windows-style Checkboxes.
  • A Mac application has Mac-style Buttons and Mac-style Checkboxes.

Now, the problem:

  • You need a way to create buttons and checkboxes, but ensure that they match the environment (Windows or Mac).
  • If you mix them accidentally (e.g., Windows Button + Mac Checkbox), the UI will look inconsistent.

If you directly hardcode object creation like this:

if (os.equals("Windows")) {
    Button btn = new WindowsButton();
    Checkbox chk = new MacCheckbox(); // mistake: mixed!
}

…it becomes messy, error-prone, and violates loose coupling.

Solution with Abstract Factory Pattern

The Abstract Factory Pattern groups related factories into one “super factory” (an abstract factory).

  • Define abstract products (e.g., Button, Checkbox).
  • Define an abstract factory that declares creation methods for these products.
  • Implement concrete factories for each environment (Windows, Mac).
  • Clients only depend on the abstract factory, never on concrete classes.

This way, the client always gets a consistent family of products.

Implementation in Java

Step 1: Abstract Product Interfaces

// Product A
public interface Button {
    void render();
}

// Product B
public interface Checkbox {
    void render();
}

Step 2: Concrete Products (Windows Family)

public class WindowsButton implements Button {
    @Override
    public void render() {
        System.out.println("Rendering a Windows-style button");
    }
}

public class WindowsCheckbox implements Checkbox {
    @Override
    public void render() {
        System.out.println("Rendering a Windows-style checkbox");
    }
}

Step 3: Concrete Products (Mac Family)

public class MacButton implements Button {
    @Override
    public void render() {
        System.out.println("Rendering a Mac-style button");
    }
}

public class MacCheckbox implements Checkbox {
    @Override
    public void render() {
        System.out.println("Rendering a Mac-style checkbox");
    }
}

Step 4: Abstract Factory

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

Step 5: Concrete Factories

public class WindowsFactory implements GUIFactory {
    @Override
    public Button createButton() {
        return new WindowsButton();
    }

    @Override
    public Checkbox createCheckbox() {
        return new WindowsCheckbox();
    }
}

public class MacFactory implements GUIFactory {
    @Override
    public Button createButton() {
        return new MacButton();
    }

    @Override
    public Checkbox createCheckbox() {
        return new MacCheckbox();
    }
}

Step 6: Client Code

public class Application {
    private Button button;
    private Checkbox checkbox;

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

    public void renderUI() {
        button.render();
        checkbox.render();
    }

    public static void main(String[] args) {
        // Decide the environment at runtime
        String os = "Mac"; // could come from config, system property, etc.

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

        Application app = new Application(factory);
        app.renderUI();
    }
}

Output (if Mac is selected)

Rendering a Mac-style button
Rendering a Mac-style checkbox

Output (if Windows is selected)

Rendering a Windows-style button
Rendering a Windows-style checkbox

Notice how:

  • The client (Application) doesn’t know or care about WindowsButton or MacCheckbox.
  • The factory guarantees that both Button and Checkbox belong to the same family.

Real-World Analogy

Think of a furniture shop.

  • You don’t want to mix a Victorian chair with a Modern sofa — they should match.
  • A Victorian Furniture Factory creates Victorian-style products (chairs, sofas, tables).
  • A Modern Furniture Factory creates modern-style products.

When you pick a style, the factory ensures consistency.

Advantages of Abstract Factory Pattern

  1. Ensures Consistency: Creates families of related objects that work well together.
  2. Loose Coupling: Client code depends only on interfaces, not concrete classes.
  3. Open/Closed Principle: Adding a new product family (LinuxFactory) doesn’t affect existing code.
  4. Runtime Flexibility: Choose product family at runtime (Windows, Mac).

Disadvantages

  1. Complexity: More classes and interfaces compared to Factory Method.
  2. Difficult to Extend Products Individually: Adding a new product type (e.g., Slider) requires changes in all factories.

When to Use

  • When your system needs to be independent of product creation.
  • When you want to enforce that a family of related products is used together.
  • When supporting multiple product variants (themes, platforms, UI kits).

Conclusion

The Abstract Factory Pattern builds upon the Factory Method by dealing with groups of related objects. It ensures consistency across product families, making it ideal for cross-platform applications, theming engines, or plugin-based architectures.

In our example, we built a Cross-Platform UI Library where the client chooses between Windows and Mac factories, and gets a consistent look-and-feel.

Next, we will study the Singleton Design Pattern — a pattern that ensures a class has only one instance and provides a global point of access to it.

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Multi...
Advanced Java August 08 ,2025

Java Multithreading...

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Semaphore...
Advanced Java October 10 ,2025

Semaphore in Java

ExecutorSe...
Advanced Java October 10 ,2025

ExecutorService in J...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Producer-C...
Advanced Java October 10 ,2025

Producer-Consumer Pr...

Implementi...
Advanced Java October 10 ,2025

Implementing a Custo...

Busy Spin
Advanced Java October 10 ,2025

Busy Spin

Serializat...
Advanced Java October 10 ,2025

Serialization and De...

Segment Lo...
Advanced Java October 10 ,2025

Segment Locking in J...

Tree Bins...
Advanced Java October 10 ,2025

Tree Bins in Java

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com