100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the Factory Pattern?

Learn the Factory design pattern: centralized object creation, decoupled callers, and a Java example for interviews.

mediumQ14 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

The Factory pattern delegates object creation to a dedicated method or class instead of calling a constructor directly, so callers depend on an interface rather than a concrete type.

A factory method takes some input (a type code, config, or context) and returns an object implementing a common interface, hiding which concrete class was actually instantiated. This decouples client code from specific classes: adding a new product type means adding a new branch in the factory, not touching every call site that creates objects. It is the foundation for more advanced creational patterns like Abstract Factory, which produces families of related objects. The trade-off is an extra layer of indirection that can be overkill when there is only ever one concrete type.

  • Decouples client code from concrete classes
  • Centralizes creation logic in one place
  • Makes adding new types easier without touching callers
  • Supports returning different subtypes behind one interface

AI Mentor Explanation

A cricket academy has a single selection committee that, given a player’s skill profile, decides whether to hand them a fast-bowling kit, spin kit, or batting kit — the player doesn’t choose their own equipment set. The committee centralizes that decision so the rest of the academy just deals with "a kitted player." A Factory works the same way: one method decides which concrete object to build, and callers just receive the finished product.

Step-by-Step Explanation

  1. Step 1

    Define a common interface

    All products the factory can create must implement the same interface or base class.

  2. Step 2

    Write the factory method

    A static or instance method accepts input describing what’s needed.

  3. Step 3

    Branch on the input

    Inside, decide which concrete class to instantiate based on the input.

  4. Step 4

    Return the interface type

    Callers receive the interface type and never see the concrete class directly.

What Interviewer Expects

  • Explaining how the factory decouples callers from concrete classes
  • Distinguishing simple Factory Method from Abstract Factory
  • A code example showing a common interface returned by the factory
  • Recognizing when a factory is unnecessary overhead

Common Mistakes

  • Confusing Factory pattern with just any function that returns an object
  • Not returning a common interface, defeating the decoupling purpose
  • Overusing factories for classes with a single, fixed concrete type
  • Mixing up Factory Method and Abstract Factory

Best Answer (HR Friendly)

The Factory pattern centralizes object creation in one place, so instead of scattering "new SomeClass()" calls everywhere, callers ask a factory for what they need and the factory decides which concrete class to build, which makes adding new types much easier later.

Code Example

Factory method returning a common interface
interface Notification {
    void send(String message);
}

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

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

class NotificationFactory {
    static Notification create(String type) {
        switch (type) {
            case "email": return new EmailNotification();
            case "sms": return new SmsNotification();
            default: throw new IllegalArgumentException("Unknown type: " + type);
        }
    }
}

// Caller never references EmailNotification or SmsNotification directly
Notification n = NotificationFactory.create("sms");
n.send("Build passed");

Follow-up Questions

  • What is the difference between Factory Method and Abstract Factory?
  • When would a factory be unnecessary overhead?
  • How does the Factory pattern support the open-closed principle?
  • Can a factory itself be replaced by dependency injection?

MCQ Practice

1. The main purpose of the Factory pattern is to?

Factories centralize creation so callers depend only on a common interface.

2. What do all products created by a factory typically share?

A shared interface lets callers use any created product interchangeably.

3. Which pattern creates families of related objects, extending the basic Factory idea?

Abstract Factory groups multiple factory methods to build related object families.

Flash Cards

What does a Factory method do?Centralizes creation logic and returns objects behind a common interface.

Why use a Factory instead of new directly?To decouple callers from concrete classes, easing future changes.

Related more advanced pattern?Abstract Factory, for families of related objects.

When is a Factory overkill?When there is only ever one fixed concrete type to create.

1 / 4

Continue Learning