Factory Method

1. Usage

“Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.”(Gang Of Four)

2. UML class diagram

3. Pros

– client can create objects without specifying subclass types.

4. Cons

– hard to refactor existing code.
– if abstract class need to be extended all sub-classes must be extended also.

Difference to Abstract Factory:
– Factory Method used to create One type of Product but Abstract Factory about creation of families of related or dependent products;
– Factory Method expose Method to client for creating product while Abstract Factory expose family of related objects which consist of many Factory Methods;