In class-based programming, the factory method pattern is a creational pattern which uses
factory methods to deal with the problem of creating objects without specifying the exact
class of object that will be created. This is done by creating objects via a factory
method, which is either specified in an interface and implemented in implementing classes; or
implemented in a base class, which can be overridden when inherited in derived classes;
rather than by a constructor.
Definition The factory method pattern should not be confused
with the more general notion of factories and factory methods. The factory method pattern
is the best-known use of factories and factory methods, but not all uses of factory methods
are examples of the factory method pattern – only when inheritance is involved is it
an example of the factory method pattern. More basic uses of factories are not examples
of the factory method pattern, and may instead be referred to as the factory pattern or a
simple factory; these are discussed at factory. The essence of this pattern is to "Define
an interface for creating an object, but let the classes that implement the interface decide
which class to instantiate. The Factory method lets a class defer instantiation to subclasses."
Creating an object often requires complex processes not appropriate to include within
a composing object. The object's creation may lead to a significant duplication of code,
may require information not accessible to the composing object, may not provide a sufficient
level of abstraction, or may otherwise not be part of the composing object's concerns.
The factory method design pattern handles these problems by defining a separate method
for creating the objects, which subclasses can then override to specify the derived type
of product that will be created. The factory method pattern relies on inheritance,
as object creation is delegated to subclasses that implement the factory method to create
objects. Example implementations
Java A maze game may be played in two modes, one
with regular rooms that are only connected with adjacent rooms, and one with magic rooms
that allow players to be transported at random. The regular game mode could use this template
method:
In the above snippet, the MazeGame constructor is a template method that makes some common
logic. It refers to the makeRoom factory method that encapsulates the creation of rooms such
that other rooms can be used in a subclass. To implement the other game mode that has
magic rooms, it suffices to override the makeRoom method:
PHP Another example in PHP follows, this time
using interface implementations as opposed to subclassing. It is important to note that
the factory method can also be defined as public and called directly by the client code.
Limitations There are three limitations associated with
the use of the factory method. The first relates to refactoring existing code; the other two
relate to extending a class. The first limitation is that refactoring an
existing class to use factories breaks existing clients. For example, if class Complex were
a standard class, it might have numerous clients with code like:
Once we realize that two different factories are needed, we change the class. But since
the constructor is now private, the existing client code no longer compiles.
The second limitation is that, since the pattern relies on using a private constructor, the
class cannot be extended. Any subclass must invoke the inherited constructor, but this
cannot be done if that constructor is private. The third limitation is that, if the class
were to be extended, the subclass must provide its own re-implementation of all factory methods
with exactly the same signatures. For example, if class StrangeComplex extends Complex, then
unless StrangeComplex provides its own version of all factory methods, the call
will yield an instance of Complex rather than the expected instance of the subclass. The
reflection features of some languages can avoid this issue.
All three problems could be alleviated by altering the underlying programming language
to make factories first-class class members. Uses
In ADO.NET, IDbCommand.CreateParameter is an example of the use of factory method to
connect parallel class hierarchies. In Qt, QMainWindow::createPopupMenu is a factory
method declared in a framework that can be overridden in application code.
In Java, several factories are used in the javax.xml.parsers package. e.g. javax.xml.parsers.DocumentBuilderFactory
or javax.xml.parsers.SAXParserFactory. See also
Design Patterns, the highly influential book Design pattern, overview of design patterns
in general Abstract factory pattern, a pattern often
implemented using factory methods Builder pattern, another creational pattern
Template method pattern, which may call factory methods
References
Fowler, Martin; Kent Beck, John Brant, William Opdyke, and Don Roberts. Refactoring: Improving
the Design of Existing Code. Addison-Wesley. ISBN 0-201-48567-2.
Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John. Design Patterns: Elements
of Reusable Object-Oriented Software. Addison-Wesley. ISBN 0-201-63361-2.
Cox, Brad J.;. Object-oriented programming: an evolutionary approach. Addison-Wesley.
ISBN 978-0-201-10393-9. Cohen, Tal; Gil, Joseph. "Better Construction
with Factories". Journal of Object Technology. Retrieved 2007-03-12.
External links Factory method in UML and in LePUS3
Consider static factory methods by Joshua Bloch