Factory Method Design Pattern
Creational pattern. Creational design patterns are concerned with object creation.
The Factory Method design pattern solves problems like: How can an object be created so that subclasses can redefine which class to instantiate?
Open in the interactive referenceDownload the PDF reference
Problem
The Factory Method design pattern solves problems like:
How can an object be created
so that subclasses can redefine which class to instantiate?
How can a class defer instantiation to subclasses?
See Applicability section for all problems Factory Method can solve.
See Solution section for how Factory Method solves the problems.
-
An inflexible way is to create an object
(
new Product1()) directly within the class (Creator|operation()) that requires (uses) the object. - This commits (couples) the class to a particular object and makes it impossible to change the instantiation (which class to instantiate) independently from (without having to change) the class.
- That's the kind of approach to avoid if we want to create an object so that subclasses can redefine the way the object is created.
-
For example, designing reusable classes that require (depend on) other objects.
A reusable class should avoid creating the objects it requires directly (and often it doesn't know which class to instantiate) so that users of the class can write subclasses to specify the instantiation they need.
Solution
The Factory Method design pattern provides a solution:
Define a separate operation (factory method)
for creating an object.
Create an object by calling a factory method.
Describing the Factory Method
design in more detail is the theme of the following sections.
See Applicability section for all problems
Factory Method can solve.
- The key idea in this pattern is to create an object in a separate operation so that subclasses can redefine which class to instantiate if necessary.
-
Define a separate
factory method:
- The pattern calls a (separate) operation that is (exclusively) responsible for "manufacturing" an object a factory method. [GoF, p108]
-
Create an objects by calling a
factory method
(Product product = factoryMethod()).
-
This enables compile-time flexibility (via subclassing).
Subclasses can be written to redefine the way an object is created. - "People often use Factory Method as the standard way to create objects, but it isn't necessary when the class that's instantiated never changes […]" [GoF, p136]
- Note that the Factory Method pattern can be implemented differently (abstract, concrete, or static factory method). See Implementation.
Applicability
Design Problems
-
Creating Objects
-
How can an object be created
so that subclasses can redefine which class to instantiate? - How can a class defer instantiation to subclasses?
-
How can an object be created
-
Flexible Alternative to Constructors
- How can a flexible alternative be provided to direct constructor calls?
Refactoring Problems
- Unclear Code
Background Information
-
Inflexible constructor names can cause unclear code.
-
In most languages, the constructor of a class
must be named after the class.
If a class has multiple constructors, they all must have the same name, which makes it hard to distinguish them and call the right one. - Factory methods can be named freely to clearly communicate their intent.
-
In most languages, the constructor of a class
must be named after the class.
-
"Consider static factory methods instead of constructors."
[JB08, Item 1]
See also Implementation. -
Refactoring and "Bad Smells in Code"
[MF99]
[JK05]
- Code smells are certain structures in the code that "smell bad" and indicate problems that can be solved by a refactoring.
-
The most common code smells are:
complicated code (including complicated/growing conditional code),
duplicated code,
inflexible code (that must be changed whenever requirements change), and
unclear code (that doesn't clearly communicate its intent).
Not included on this page: Intent, Motivation, Structure, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.