Abstract Factory Design Pattern in Java with Examples | Beginner to Advanced Guide

Reading Time: 20 minutes

The Abstract Factory Design Pattern is a creational design pattern that provides an interface to create families of related objects without specifying their concrete classes.

It is an extension of the Factory Pattern. While a Factory creates one type of object, an Abstract Factory creates a group of related objects that work together.

This pattern is widely used in large enterprise systems where multiple related components must be created consistently.

This tutorial explains the Abstract Factory pattern in a beginner-friendly way with simple examples, step-by-step implementation, real-world understanding, and interview-level concepts.

What is Abstract Factory Pattern

The Abstract Factory pattern provides a way to create families of related objects without directly creating their concrete classes.

Simple definition:

Abstract Factory creates multiple related objects together using a factory interface. Instead of creating individual objects, we create a factory that produces related objects.

Understanding the Core Idea

Factory Pattern creates one product, whereas Abstract Factory creates multiple related products.

Example Comparison

Factory → Creates one payment object.
Abstract Factory → Creates complete UI components like Button and Checkbox for a specific operating system.

The main goal is to ensure that related objects are used together correctly.

Why Do We Need Abstract Factory Pattern

In complex systems, multiple objects are related and must be used together.

Example Scenario

A graphical user interface application may support different operating systems:

Windows UI → Windows Button, Windows Checkbox
Mac UI → Mac Button, Mac Checkbox

If we create these objects manually using conditions, the code becomes complex and tightly coupled.

Problem Without Abstract Factory


package com.codekatha;

class Application {

    public void createUI(String osType) {

        if (osType.equals("WINDOWS")) {
            WindowsButton button = new WindowsButton();
            WindowsCheckbox checkbox = new WindowsCheckbox();
        }
        else if (osType.equals("MAC")) {
            MacButton button = new MacButton();
            MacCheckbox checkbox = new MacCheckbox();
        }
    }
}

Problems in This Approach

Large conditional logic
Tight coupling
Difficult to add new operating systems
Hard to maintain and extend

Solution – Abstract Factory Pattern

The Abstract Factory pattern provides a factory interface that creates related objects. Concrete factories implement this interface and create specific product families.

The client uses the factory without knowing which objects are created.

Key Components of Abstract Factory

Abstract Products → Interfaces for related objects
Concrete Products → Actual implementations
Abstract Factory → Interface for creating products
Concrete Factory → Creates product family
Client → Uses factory

Step-by-Step Abstract Factory Implementation

Step 1 – Create Abstract Product Interfaces

These define the common behavior for product families.


package com.codekatha;

interface Button {
    void render();
}

interface Checkbox {
    void check();
}

Step 2 – Create Concrete Product Classes

Each platform provides its own implementations.


package com.codekatha;

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

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

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

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

Step 3 – Create Abstract Factory Interface

This interface defines methods to create related objects.


package com.codekatha;

interface GUIFactory {

    Button createButton();

    Checkbox createCheckbox();
}

Step 4 – Create Concrete Factory Classes

Each factory produces a specific product family.


package com.codekatha;

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

The client uses the factory without knowing product details.


package com.codekatha;

class AbstractFactoryDemo {

    public static void main(String[] args) {

        GUIFactory factory = new WindowsFactory();

        Button button = factory.createButton();
        Checkbox checkbox = factory.createCheckbox();

        button.render();
        checkbox.check();
    }
}

What We Achieved Using Abstract Factory

Related objects created together
Loose coupling between client and concrete classes
Consistent product family usage
Easy system extension

Real-Life Example of Abstract Factory

Think of a furniture store.

Modern furniture factory → Modern Chair, Modern Table
Victorian furniture factory → Victorian Chair, Victorian Table

Each factory creates a complete family of related products.

Abstract Factory vs Factory Pattern

Factory Pattern creates a single object.
Abstract Factory creates a family of related objects.

Example Comparison

Factory → Creates one Payment object.
Abstract Factory → Creates UI components like Button and Checkbox.

Advantages of Abstract Factory Pattern

Ensures product compatibility
Promotes loose coupling
Supports scalability
Encapsulates object creation logic
Improves maintainability

Disadvantages of Abstract Factory Pattern

Complex structure
More classes required
Hard to add new product types

When Should You Use Abstract Factory Pattern

When system must create families of related objects
When products must work together consistently
When object creation logic is complex
When system should be independent of concrete classes

Real-World Usage of Abstract Factory

Cross-platform UI frameworks
Database driver families
Theme or style systems
Spring BeanFactory concept
Enterprise application frameworks

Common Interview Questions on Abstract Factory

Difference between Factory and Abstract Factory?
When to use Abstract Factory?
How Abstract Factory supports loose coupling?
Real-world example of Abstract Factory?

Simple Summary

Abstract Factory creates families of related objects.
Client uses factory interface.
Concrete factories create product families.
Improves consistency and scalability.

Conclusion

The Abstract Factory pattern helps build systems that create related objects consistently while hiding creation details. It is useful in large applications where multiple components must work together.

Understanding this pattern improves system design and helps build flexible and scalable enterprise applications.

Comments

Popular Posts on Code Katha

Java Interview Questions for 10 Years Experience

Sql Interview Questions for 10 Years Experience

Spring Boot Interview Questions for 10 Years Experience

Java interview questions - Must to know concepts

Visual Studio Code setup for Java and Spring with GitHub Copilot

Spring AI with Ollama

Data Structures & Algorithms Tutorial with Coding Interview Questions

Spring Data JPA

Topological Sort in Graph

Bit Manipulation and Bit Masking Concepts