How does Abstract Factory differ from Factory Method in Java API design, with JDK examples of each?
answer
- Factory Method = one product; Abstract Factory = a family
- Method vs. dedicated object
- Calendar.getInstance() vs. DocumentBuilderFactory
- Each method of an Abstract Factory IS a factory method
- Abstract Factory guarantees product consistency
basics
~10 sFactory Method is one method that creates one product (e.g. Calendar.getInstance()). Abstract Factory is an object with several creation methods that build a whole family of related products together (e.g. DocumentBuilderFactory).
solid answer
~50 sBoth hide concrete classes behind a creation API, but they differ in scope and structure. Factory Method is a single method that returns one product, where subclasses (or a static method) decide the concrete type — for example Calendar.getInstance() or NumberFormat.getInstance() return a locale-appropriate concrete instance. Abstract Factory is a whole object dedicated to creation, exposing several methods that together produce a family of related, mutually-consistent products — for example DocumentBuilderFactory or SAXParserFactory, whose factory object creates builders/parsers from one consistent provider. The rule of thumb: Factory Method = one product per call, often via inheritance/static method; Abstract Factory = an object that creates a family, often via composition (you hold the factory and call multiple creators). Abstract Factory is frequently *built out of* Factory Methods — each creation method on the factory is itself a factory method. So they compose rather than compete.
go deeper
Knows that both hide concrete classes, and can name Calendar.getInstance() for Factory Method and DocumentBuilderFactory for Abstract Factory.
Clearly states the one-product-vs-family distinction and the method-vs-object distinction, with a correct JDK example for each.
Explains that each Abstract Factory creation method is itself a factory method, that they compose, and articulates the consistency guarantee and when to prefer each.
Reasons about API evolution costs (adding a product type to a family vs. a new factory variant), how the static-factory + abstract-factory layering in JAXP enables pluggability, and how DI frameworks subsume much of this in modern code.
## Two creational patterns, often confused Both **Factory Method** and **Abstract Factory** exist to do the same broad thing: let client code obtain objects *without* hardcoding (`new ConcreteClass()`) the concrete type. They differ in **scope** (one product vs. a family) and in **mechanism** (a method vs. a dedicated object). ### Factory Method - **Definition:** a method whose job is to create and return one product, deferring the choice of concrete class. In the GoF form, the method is `abstract`/overridable and *subclasses* decide what to instantiate. In everyday Java, the term also covers **static factory methods** (Effective Java sense) like `getInstance()`/`of()`/`valueOf()`. - **Scope:** **one** product per call. - **JDK examples:** - `Calendar.getInstance()` — returns a concrete `Calendar` (e.g. `GregorianCalendar`) appropriate to the default locale/timezone; the caller never names the subclass. - `NumberFormat.getInstance()` / `getCurrencyInstance()` — returns a locale-appropriate concrete `NumberFormat`. - `Collections.unmodifiableList(...)`, `Optional.of(...)`, `Integer.valueOf(...)` — static factory methods that hide (and may cache or vary) the concrete return type. ### Abstract Factory - **Definition:** an **object** (interface or abstract class) that declares **several** creation methods, one per product type. A concrete factory implements them all to return one **consistent family** of concrete products. - **Scope:** a **family** of related products that must work together. - **JDK examples:** - `DocumentBuilderFactory` → `newDocumentBuilder()` produces a DOM `DocumentBuilder` (and the parser/DOM objects under it) from one provider. - `SAXParserFactory` → `newSAXParser()` produces a `SAXParser`/`XMLReader` family. - `XPathFactory`, `TransformerFactory`, `XMLInputFactory` — same idea across JAXP. ### The key contrasts | Aspect | Factory Method | Abstract Factory | |---|---|---| | Unit | A single method | A whole object dedicated to creation | | Output | One product | A family of related products | | Typical mechanism | Inheritance/override, or a static method | Composition — you hold the factory, call several creators | | Consistency guarantee | n/a (one product) | All products come from one provider, guaranteed compatible | | JDK example | `Calendar.getInstance()` | `DocumentBuilderFactory.newInstance()` | ### They compose, not compete Notice that **each creation method on an Abstract Factory is itself a Factory Method.** `DocumentBuilderFactory.newDocumentBuilder()` is a factory method living *inside* an abstract factory. So the real distinction is whether the design groups multiple related creators into one object (Abstract Factory) or exposes a single creator (Factory Method). When you only need one product type, Factory Method is simpler; when you need a coordinated set, Abstract Factory. ### A subtlety with `newInstance()` `DocumentBuilderFactory.newInstance()` is itself a static factory method that returns *the factory* (provider-selected). Then the returned factory's `newDocumentBuilder()` creates the actual product. So JAXP layers a static factory (to get the abstract factory) on top of an abstract factory (to get the products) — a common real-world combination. ### Why interviewers ask this They want to see that you don't treat "factory" as one undifferentiated word. Demonstrate the scope difference (one vs. family), give a JDK example of each, and note that Abstract Factory's methods are factory methods — that nuance signals real understanding.
- Is Calendar.getInstance() a GoF Factory Method or a static factory method?Strictly it's a static factory method (Effective Java sense) — a static method that hides the concrete subtype and selects a locale-appropriate Calendar. It's commonly cited as 'Factory Method' loosely because it defers concrete-class choice, but it's not the inheritance-based GoF variant where subclasses override the creator.
- Can Abstract Factory and Factory Method appear together?Yes — they typically do. Each creation method on an Abstract Factory (e.g. newDocumentBuilder()) is a factory method, and DocumentBuilderFactory.newInstance() is itself a static factory that returns the abstract factory. They compose rather than compete.
saying these in an interview costs you the question
- Saying they're the same pattern with different names — scope differs (one product vs. a family).
- Claiming Abstract Factory always uses inheritance — clients usually use it by composition (holding the factory object).
- Forgetting the consistency guarantee that makes Abstract Factory worth its extra structure.
- Treating 'factory method' and 'static factory method' as identical to the GoF Factory Method — the GoF version is inheritance-based.