Factory Method Pattern
Factory Method delegates object creation to a method whose implementation can vary, allowing client code to work with an abstraction instead of constructing concrete classes directly. In Java, this commonly means a superclass defines the creation contract while subclasses decide which concrete product to instantiate.
The pattern separates object creation from object use. A creator exposes a factory method that returns a product abstraction, while concrete creators override that method to choose a particular implementation.
The client typically depends on the product interface rather than a concrete product class. This reduces coupling: adding another product can often be done by introducing another creator and product implementation without changing the client logic.
Factory Method is especially useful when a framework or base workflow is stable but the concrete object used by that workflow varies. The base creator can contain common operations that call the factory method, while subclasses customize only the creation step.
In Java, the factory method may be abstract or overridable, and it can return an interface or superclass type. The pattern should not be confused with a simple static factory method; the defining idea is that creation is designed for variation through overriding or polymorphism.
Key points
- Define a product abstraction such as an interface or abstract class.
- Put the creation operation behind a factory method.
- Let concrete creators determine which product implementation is created.
- Keep client code dependent on product abstractions rather than concrete classes.
- Use the pattern when object creation needs to vary independently from the surrounding workflow.
Interview questions
Why is Factory Method useful for extensibility?
It allows a stable workflow to remain unchanged while new product implementations are introduced. A new concrete creator can select the new product through the existing factory method contract. This can reduce modifications to existing client or workflow code. The benefit is strongest when the creation decision is a genuine variation point rather than an artificial abstraction.
What problem does the Factory Method pattern solve?
It separates object creation from the code that uses the object. A creator exposes a factory method that returns an abstraction, and concrete creators decide which implementation to instantiate. This lets the common workflow remain unchanged when the concrete product varies. It also reduces direct coupling between clients and concrete product classes.
How would you implement Factory Method in Java?
I would define a product interface or abstract class and a creator class containing a factory method that returns that product type. The creator's business operation uses the returned abstraction instead of directly constructing a concrete product. Concrete creator subclasses override the factory method and return their chosen implementation. This keeps the creation decision behind a polymorphic boundary.
What is the difference between Factory Method and Abstract Factory?
Factory Method focuses on creating a product through a method that can be specialized by concrete creators. Abstract Factory provides an interface for creating a family of related products, usually with several factory operations. Factory Method commonly relies on inheritance or overriding, while Abstract Factory commonly relies on composition through a factory object. They can also be combined in real designs.
What are the disadvantages of Factory Method?
The main cost is additional structure: introducing creators and subclasses can make a small design more complicated than necessary. If every new product requires another creator subclass, the class hierarchy can grow quickly. Dependency injection, a registry, or a simple factory may be clearer when the selection mechanism does not naturally belong to a creator hierarchy. The pattern is most valuable when polymorphic creation is itself an important design requirement.