Creational Design Pattern

Factory Method Design Pattern in Java

Define an interface for creating an object and let subclasses decide which concrete object to create.

In this lesson: Learn the Factory Method structure, the creator and product roles, a Java implementation, and when this pattern is useful.

What is Factory Method?

The Factory Method pattern moves object creation out of client code. A creator class declares a method for creating a product, while concrete creator classes override that method to return a specific product implementation.

The client works with product and creator abstractions instead of directly constructing concrete classes. This makes the code easier to extend when new product types are introduced.

The object creation problem

Direct construction couples an application to a concrete implementation. Every new notification type, payment provider, parser, or report format can force changes in the code that uses it.

public void sendNotification(String type, String message) {
    if ("email".equals(type)) {
        new EmailNotification().send(message);
    } else if ("sms".equals(type)) {
        new SmsNotification().send(message);
    }
}

This conditional logic works for a small example, but it grows as more product types and creation rules are added.

Factory Method implementation

First, define a product interface and concrete products. The creator owns the workflow and delegates the product creation step to a factory method.

interface Notification {
    void send(String message);
}

final class EmailNotification implements Notification {
    public void send(String message) {
        System.out.println("Email: " + message);
    }
}

final class SmsNotification implements Notification {
    public void send(String message) {
        System.out.println("SMS: " + message);
    }
}

Next, define the creator and its concrete creators.

abstract class NotificationService {
    protected abstract Notification createNotification();

    public void notifyUser(String message) {
        Notification notification = createNotification();
        notification.send(message);
    }
}

final class EmailNotificationService extends NotificationService {
    protected Notification createNotification() {
        return new EmailNotification();
    }
}

final class SmsNotificationService extends NotificationService {
    protected Notification createNotification() {
        return new SmsNotification();
    }
}

The application can now use the creator abstraction while each concrete service decides which notification product to create.

Roles in Factory Method

Advantages and limitations

When should you use it?

Use Factory Method when a class cannot know in advance which concrete object it should create, when subclasses need to control product creation, or when a creation decision is likely to grow over time. It is common in framework extension points, file parsers, notification providers, payment integrations, and document exporters.

Next step: Continue with the Design Patterns course and explore Abstract Factory after understanding Factory Method.