Last week, in part 1 [1], Andre Mare introduced us to the Builder pattern. Today he continues his series on the "Gang of Four" design patterns. -- Geertjan Wielenga, JavaLobby Zone Leader
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. - Gof
Class Creational
The Factory Method Pattern is a well known Creational Pattern that creates an abstraction through which one of several classes is returned. The pattern enables us to encapsulate the instantiation of concrete types. The pattern is classified as a Class Creational Pattern which means the pattern makes use of inheritance to decide what object to instantiate.
The Factory Method Pattern consists of a Product, ConcreteProduct, Creator, ConcreteCreator and Client.
The pattern makes use of polymorphism to decouple the client from the class created by the Factory Method. The Factory Method returns an instance of a class (ConcreteProduct) that is defined through an interface or abstract parent (Product) class. The method where the class is created returns the object through its interface or abstract parent class, so that the client is decoupled from the actual ConcreteProduct class. All the returned classes through the factory methods have the same type (interface) or abstract parent class.
Download : Bank Account System [2]
The following example will illustrates the use of the Factory Method pattern. The Bank Account System example illustrates the creation of different bank accounts for different banks. The first diagram illustrates the different bank account types that are available.
Product & ConcreteProductsThe Bank Account System contains an abstract type for all the bank accounts available called the BacnkAccountProduct. The other classes are ConcreteProduct classes that is created by the Factory Method, depending on the type and the ConcreteCreator class.
Creator & ConcreteCreator
The BankAccountCreator or Creator class defines the factory method as abstract so that the implementation is delegated to the subclasses. The factory method is defined as follows:
protected abstract BankAccountProduct createBankAccount(String accountType);
This factory method is then implemented in all the different subclasses of the BankAccountCreator class. Each different ConcreteCreator knows how to instantiate the different ConcreteProduct classes.
Factory Method Class Diagram
The class diagram below show the dependencies between the different classes as used in the Bank Account System example.

The BankSystemClient class has a gets a reference to an object of type BankAccountProduct. The client does not know what the implementing class is, but rather works through the abstract BankAccountProduct class.
Sequence Diagram by "Yanic Inghelbrecht" with Trace Modeler [3]
Links:
[1] http://java.dzone.com/news/design-pattern-builder-pattern
[2] http://www.javadesign.info/media/blogs/JDesign/DesignConcepts/DesignPatterns/GOF/factorymethod/FactoryMethodPattern_Example.zip
[3] http://www.tracemodeler.com/
[4] http://www.javadesign.info/DesignConcepts/DesignPatterns/GOF/factorymethod_pattern