Often a static method
Decloupes clients from implementation details of derived classes
Enables poloymorphic creation.
Arguments to method determine what object should be created, factory determins how.
Unlike a constructor could return:

  • Subclass
  • Reused object
  • More descriptive names

The factory method pattern is used to create objects.  It is useful in that encapsulates object creation from clients.

Diagram

Components

IProduct

Interface for product objects

Product

This is a concrete implementation of IProduct.

ProductFactory

ProductFactory is an abstract class that declares the factory method which returns an object of type IProduct

ProductFactoryConcrete

ProductFactoryConcrete implements the abstract ProductFactory.  It overrides the factory method and returns concrete Products.