The Factory Method pattern is extremely useful when a class cannot decide in advance the class of objects it must create. The task of specifying the objects is given to the subclasses, instead. The class delegates responsibility to one of several helper subclasses, and the user localizes the knowledge of the delegate.

Factory Method Pattern

There can exist slight variations of the Factory Method.

The advantage of this pattern is:

  • The need to bind very specific classes to the code does not exist anymore. The exact class of the object that will be created need not be specified anymore.

The disadvantage of this pattern is:

  • Clients may have to subclass the FactoryCreatorInterface, to just create a specific Product object.

Examples

These are some examples from the internet. More will follow, as we receive contributions.

References