Lesson 44: Design Pattern: Factory

14 Aug 2026 1 min Swarnil Singhai

Factory centralizes object creation logic so callers don't need to know which concrete class to instantiate — just what they need it to do.

Real-time example

A notification system's NotifierFactory decides whether to create an EmailNotifier or SmsNotifier based on user preference — callers never say new.

interface Notifier { void send(String msg); }
class EmailNotifier implements Notifier {
    public void send(String msg) { System.out.println("Email: " + msg); }
}
class SmsNotifier implements Notifier {
    public void send(String msg) { System.out.println("SMS: " + msg); }
}
class NotifierFactory {
    static Notifier create(String type) {
        return type.equals("sms") ? new SmsNotifier() : new EmailNotifier();
    }
}

What's happening

Callers depend only on the Notifier interface and NotifierFactory — adding a PushNotifier later touches only the factory.

Factories are the standard answer to 'this switch/if-chain of new X() keeps growing' — centralize it once.

Advertisement