Home  Java   Factory and ...

factory and abstract factory patterns in Java

Both the Factory Method and Abstract Factory are Creational Design Patterns used to control object creation, but they solve problems at different levels of abstraction.


1. Factory Method Pattern 🏭

The Factory Method pattern provides an interface for creating an object in a superclass, but allows subclasses to alter the type of object that will be created. It's used when you want to delegate object creation to subclasses.

Real-Life Analogy: Digital Document Creation

Imagine an application that can create different types of digital documents. The core application doesn't need to know the specific steps to create a Word Document versus a PDF Document; it just knows it needs a new document object.

Java Example

The factory method is the createDocument() method.

ComponentRole
DocumentProduct interface (e.g., WordDocument, PdfDocument).
ApplicationCreator interface/abstract class. Declares the createDocument() factory method.
WordApplicationConcrete Creator. Implements createDocument() to return a specific WordDocument.
// 1. Product Interface
interface Document {
    void open();
}

// 2. Concrete Products
class WordDocument implements Document {
    public void open() { System.out.println("Opening MS Word Document."); }
}

class PdfDocument implements Document {
    public void open() { System.out.println("Opening PDF Document."); }
}

// 3. Creator Abstract Class (Declares the Factory Method)
abstract class Application {
    // This is the Factory Method: delegated to subclasses
    public abstract Document createDocument();

    // Core logic uses the Document produced by the factory method
    public void newDocument() {
        Document doc = createDocument();
        doc.open();
    }
}

// 4. Concrete Creator
class WordApplication extends Application {
    @Override
    public Document createDocument() {
        return new WordDocument(); // Creates a specific product
    }
}

// 5. Concrete Creator
class PdfApplication extends Application {
    @Override
    public Document createDocument() {
        return new PdfDocument(); // Creates a specific product
    }
}

Usage:

Application app = new WordApplication();
app.newDocument(); // Output: Opening MS Word Document.

2. Abstract Factory Pattern 🏰

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It's used when you need to ensure the created objects are always compatible with one another.

Real-Life Analogy: Cross-Platform GUI Toolkits

Imagine a software application that needs to look native on both Windows and macOS. When running on Windows, it must use all Windows components (Buttons, Checkboxes); when running on macOS, it must use all Mac components.

Java Example

ComponentRole
GUIFactoryAbstract Factory. Declares methods to create a family of products (createButton(), createCheckbox()).
ButtonAbstract Product A.
CheckboxAbstract Product B.
WindowsFactoryConcrete Factory. Creates the entire family of Windows products (WinButton, WinCheckbox).
MacFactoryConcrete Factory. Creates the entire family of Mac products (MacButton, MacCheckbox).
// --- Abstract Products ---
interface Button { void paint(); }
interface Checkbox { void paint(); }

// --- Concrete Product Family 1 (Windows) ---
class WinButton implements Button {
    public void paint() { System.out.println("Rendering a Windows Button."); }
}
class WinCheckbox implements Checkbox {
    public void paint() { System.out.println("Rendering a Windows Checkbox."); }
}

// --- Concrete Product Family 2 (Mac) ---
class MacButton implements Button {
    public void paint() { System.out.println("Rendering a Mac Button."); }
}
class MacCheckbox implements Checkbox {
    public void paint() { System.out.println("Rendering a Mac Checkbox."); }
}

// --- Abstract Factory ---
interface GUIFactory {
    Button createButton();
    Checkbox createCheckbox();
}

// --- Concrete Factory 1 (Creates the Windows family) ---
class WindowsFactory implements GUIFactory {
    public Button createButton() { return new WinButton(); }
    public Checkbox createCheckbox() { return new WinCheckbox(); }
}

// --- Concrete Factory 2 (Creates the Mac family) ---
class MacFactory implements GUIFactory {
    public Button createButton() { return new MacButton(); }
    public Checkbox createCheckbox() { return new MacCheckbox(); }
}

Usage (Client Code):

// Client uses the factory, not the concrete classes
GUIFactory factory = new WindowsFactory();
Button button = factory.createButton();
Checkbox checkbox = factory.createCheckbox();

button.paint();   // Output: Rendering a Windows Button.
checkbox.paint(); // Output: Rendering a Windows Checkbox.

Summary of Differences

FeatureFactory MethodAbstract Factory
PurposeCreates a single object (Product) through delegation.Creates families of related objects (Products A, B, C...).
FocusClass inheritance (subclasses decide what to instantiate).Object composition (client code uses one factory object to get compatible products).
Best ForWhen a class doesn't know what subclasses need to create.Ensuring that the products created are always used together and are compatible.
Published on: Oct 05, 2025, 11:00 AM  
 

Comments

Add your comment