DEV Community

Gowtham Kalyan
Gowtham Kalyan

Posted on

Factory Pattern vs Abstract Factory Pattern in Java

Factory Pattern vs Abstract Factory Pattern in Java

Design patterns help developers write clean, reusable, and maintainable code. Two commonly used creational design patterns are the Factory Pattern and the Abstract Factory Pattern. While both deal with object creation, they serve different purposes in software design.

Understanding the difference between these patterns is essential when designing scalable enterprise applications.


What is Factory Pattern?

The Factory Pattern (Factory Method Pattern) provides a single method to create objects without exposing the object creation logic to the client.

Instead of using the new keyword directly, the client asks a factory class to create the object.

Example

interface Shape {
    void draw();
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

class Rectangle implements Shape {
    public void draw() {
        System.out.println("Drawing Rectangle");
    }
}

class ShapeFactory {

    public Shape getShape(String type) {

        if(type.equalsIgnoreCase("circle")) {
            return new Circle();
        }
        else if(type.equalsIgnoreCase("rectangle")) {
            return new Rectangle();
        }

        return null;
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage

ShapeFactory factory = new ShapeFactory();
Shape shape = factory.getShape("circle");
shape.draw();
Enter fullscreen mode Exit fullscreen mode

Key Idea

The Factory class decides which object to create.


What is Abstract Factory Pattern?

The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Instead of a single factory, we create multiple factories, each responsible for creating related objects.

Example

interface Button {
    void paint();
}

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

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

interface GUIFactory {
    Button createButton();
}

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

class MacFactory implements GUIFactory {
    public Button createButton() {
        return new MacButton();
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage

GUIFactory factory = new WindowsFactory();
Button button = factory.createButton();
button.paint();
Enter fullscreen mode Exit fullscreen mode

Key Idea

Each factory creates a group of related objects.


Key Differences Between Factory and Abstract Factory

Feature Factory Pattern Abstract Factory Pattern
Purpose Creates a single object Creates families of related objects
Complexity Simple More complex
Number of factories One factory class Multiple factory classes
Object relationships Usually unrelated objects Related objects
Flexibility Less flexible Highly flexible

Real-World Example

Factory Pattern

A Notification Factory that creates:

  • Email Notification
  • SMS Notification
  • Push Notification

The factory decides which notification object to create.


Abstract Factory Pattern

A UI Component Factory for different operating systems:

Windows Factory creates:

  • Windows Button
  • Windows Checkbox

Mac Factory creates:

  • Mac Button
  • Mac Checkbox

Each factory produces a family of related UI components.


When to Use Each Pattern

Use Factory Pattern When

  • You need to create one type of object
  • Object creation logic needs to be centralized
  • You want to hide instantiation from the client

Use Abstract Factory Pattern When

  • You need to create families of related objects
  • The system must be independent of object creation
  • Multiple related objects must be used together

Why These Patterns Matter in System Design

In large-scale applications, design patterns like Factory and Abstract Factory help achieve:

  • Loose coupling
  • Scalability
  • Maintainable architecture
  • Reusable code components

These concepts are widely used in enterprise frameworks and large distributed systems.


🚀 Master Design Patterns in System Architecture

Understanding patterns like Factory, Abstract Factory, Singleton, Builder, and Prototype is essential for designing robust backend systems.

If you want to learn these concepts with real-world examples and architectural case studies, explore:

👉 No 1 System Design with Java Online Training

Top comments (0)