All topics
library
intermediate

Factory Method & Abstract Factory

Use factory patterns to decouple object creation from usage, and recognize them in JDK APIs.

A factory hides new behind a method, so the caller depends on a type rather than a class.

Factory = ordering food from a menu. You ask for 'pasta' (factory method) without knowing which chef makes it or how. The kitchen (factory) decides the recipe (implementation). You get the dish (product) without entering the kitchen.

Key Concepts

1
    List<String> a = List.of("x");            // returns some List
    Integer i = Integer.valueOf(127);          // may return a cached one
    LocalDate d = LocalDate.of(2026, 3, 15);
    Optional<T> o = Optional.ofNullable(x);
2
What a static factory can do that a constructor cannot.
    have a name          valueOf, of, from -- not four overloads of new
    return a subtype     the declared type is an interface
    return a cached object  Integer.valueOf(127) is shared
    return nothing new   Optional.empty() is a singleton
    choose the implementation from the arguments
3
The caching point is observable:
    Integer.valueOf(127) == Integer.valueOf(127);    // true, cached
    new Integer(127) == new Integer(127);            // false, and deprecated
4
A constructor must return a new object. A factory need not, which is the whole reason valueOf exists.
valueOf
5
The naming conventions.
    of / from       a conversion or a short construction
    valueOf         a more verbose conversion
    getInstance     returns an instance, maybe cached
    newInstance     always a new one
    create          same
6
Abstract Factory is a factory of factories — an interface with several creation methods, implemented per family:
7
    interface UiFactory {
      Button  button();
      Checkbox checkbox();
    }
    class DarkUi implements UiFactory { ... }
    class LightUi implements UiFactory { ... }
8
One choice of factory fixes a consistent set of products, so you cannot mix a dark button with a light checkbox. The JDK does this with DocumentBuilderFactory and TransformerFactory.
DocumentBuilderFactoryTransformerFactory
9
Factory Method, strictly, is the subclass version: a superclass declares the creating method and each subclass decides what to build. In practice "factory method" usually means the static kind above.
10
Effective Java Item 1 is the summary: prefer static factory methods to constructors.
11
And in Spring, an @Bean method is exactly a factory method, with the container calling it and caching the result.
@Bean
12
What the interviewer is probing.1. "Why prefer a static factory over a constructor?" Probing: whether you can list real advantages. Stalls: "It is tidier." Moves up: it has a name, can return a subtype, can return a cached or existing instance, and can choose an implementation from the arguments.
13
2. "Which JDK factory demonstrates caching?" Probing: a concrete example. Stalls: "None." Moves up: Integer.valueOf, which returns a shared instance for -128 to 127 — a constructor could not.
14
3. "What distinguishes Abstract Factory from Factory Method?" Probing: the two patterns people merge. Stalls: "They are the same." Moves up: Abstract Factory is an interface creating a family of related products so they cannot be mixed; Factory Method is one creating method, often deferred to a subclass.
15
4. "What are the naming conventions?" Probing: recognition. Stalls: "create." Moves up: of and from for conversions, valueOf, getInstance for a possibly cached one, newInstance for always new.