Intent
subclasses decide which class to instantiate. Factory Method lets a class defer
instantiation to sub-classes.
In 30 seconds
object creation is deferred to subclasses when the knowledge of the kind of
object to be created is not available. For example in high-level framework,
abstract classes application-create specific concrete product classes can't be known.
Frameworks use abstract classes to define and maintain relationship between
objects. When applications create specific subclasses of the framework abstract
classes, they override the factory method and create the specific object
required. Thus Factory Methods eliminates the need to bind application-specific
classes into your framework code.
Motivation
Creating objects inside a class with factory method is always more flexible than
creating an object directly. As the Abstract class can provide default object,
and the subclass can provide extended version of an object. It is often used in
Abstract Factory implementation, where there are parallel hierarchies of classes
to be crated. The Abstract Creator knows about the Product interface that is
implemented by the application specific product class and so can use that for
other operations, only the real object creation is delegated to the application
specific subclass.
Sample Code
Example:
//Abstract Creator
/* It declares the factory method that returns an object of type Product. */
abstract class Creator{
//other operations using product
abstract Product createProduct();
}
 //Concrete Creator
/* Overrides the factory method to return an instance of a ConcreteProduct */
class ConcreteCreator extends Creator{
Product createProduct(){
return new ConcreteProduct();
}
}
//Abstract Product
/* This defines the interface of objects that factory
factory method creates
*/
abstract class Product{
public abstract void execute();
}
/* Implements the Product interface */
class ConcreteProduct extends Product{
public void execute(){
System.out.println("Hello Factory Pattern");
}
}
// Client class
public class Client{
public static void main(String args<>){
//May get it through a Factory maker
ConcreteCreator creator = new ConcreteCreator();
Product p = creator.createProduct();
p.execute();
}
}
In the above example, Client class needn’t know about instance creation. The
creation of Product is deferred to the ConcreteCreator.
Complexities simplified
products, we can pass a product id to the factory method. To have one
ConcreteCreator for multiple product hierarchies we can use a template (C++
implementation) subclass, which is parameterized by the product class.
Also Known as and Related Patterns
Abstract Factory
The Factory Method pattern is useful for constructing
individual objects for a specific purpose without the construction requester
knowing the specific classes being instantiated. If you need to create a matched
set of such objects, then the Abstract Factory pattern is a more appropriate
pattern.
Template
The Factory Method pattern is often used with the Template
Method pattern.
Prototype
The Prototype pattern provides an alternate way for an
object to work with other objects without knowing the details of their
construction.
(The authors are senior developers at MindTree Consulting. They can be
reached at avijeetd@mindtree.com and
href="mailto:satyabrata_dash@mindtree.com">satyabrata_dash@mindtree.com. References:
Design Patterns, elements of reusable object-oriented software by Gamma, Helm, Johnson and Design Patterns in Java by James W. Cooper)