0
0
JavaConceptBeginner · 3 min read

Abstract Factory Pattern in Java: What It Is and How It Works

The Abstract Factory Pattern in Java is a design pattern that provides an interface to create families of related or dependent objects without specifying their concrete classes. It helps to encapsulate object creation, making code more flexible and easier to maintain.
⚙️

How It Works

Imagine you want to build different types of furniture sets, like Victorian or Modern. Each set includes matching chairs and sofas. The Abstract Factory Pattern acts like a factory that creates these sets without you needing to know the exact details of each piece.

In Java, this pattern uses an abstract factory interface with methods to create related objects. Concrete factories implement this interface to produce specific object families. This way, your code can work with any furniture set by using the abstract factory, keeping it flexible and easy to change.

💻

Example

This example shows how to use the Abstract Factory Pattern to create different types of furniture sets (Victorian and Modern) with matching chairs and sofas.

java
interface Chair {
    void sitOn();
}

interface Sofa {
    void lieOn();
}

interface FurnitureFactory {
    Chair createChair();
    Sofa createSofa();
}

class VictorianChair implements Chair {
    public void sitOn() {
        System.out.println("Sitting on a Victorian chair.");
    }
}

class VictorianSofa implements Sofa {
    public void lieOn() {
        System.out.println("Lying on a Victorian sofa.");
    }
}

class ModernChair implements Chair {
    public void sitOn() {
        System.out.println("Sitting on a Modern chair.");
    }
}

class ModernSofa implements Sofa {
    public void lieOn() {
        System.out.println("Lying on a Modern sofa.");
    }
}

class VictorianFurnitureFactory implements FurnitureFactory {
    public Chair createChair() {
        return new VictorianChair();
    }
    public Sofa createSofa() {
        return new VictorianSofa();
    }
}

class ModernFurnitureFactory implements FurnitureFactory {
    public Chair createChair() {
        return new ModernChair();
    }
    public Sofa createSofa() {
        return new ModernSofa();
    }
}

public class AbstractFactoryDemo {
    public static void main(String[] args) {
        FurnitureFactory factory = new VictorianFurnitureFactory();
        Chair chair = factory.createChair();
        Sofa sofa = factory.createSofa();
        chair.sitOn();
        sofa.lieOn();

        factory = new ModernFurnitureFactory();
        chair = factory.createChair();
        sofa = factory.createSofa();
        chair.sitOn();
        sofa.lieOn();
    }
}
Output
Sitting on a Victorian chair. Lying on a Victorian sofa. Sitting on a Modern chair. Lying on a Modern sofa.
🎯

When to Use

Use the Abstract Factory Pattern when your code needs to create families of related objects that must work together, and you want to keep the creation process flexible and independent from the actual classes.

For example, in a graphic application, you might want to support different themes (dark or light) where buttons, menus, and windows look different but share the same interface. The pattern helps you switch themes easily without changing the main code.

Key Points

  • Abstract Factory provides an interface for creating related objects without specifying their classes.
  • It promotes loose coupling by separating object creation from usage.
  • Helps manage families of related objects that must be used together.
  • Improves code flexibility and makes it easier to add new object families.

Key Takeaways

Abstract Factory Pattern creates families of related objects without specifying their concrete classes.
It helps keep code flexible and easy to maintain by separating object creation from usage.
Use it when you need to switch between different sets of related objects easily.
It promotes loose coupling and improves code organization.
Adding new object families is simpler with this pattern.