The factory is one of the most popular design patterns out there. It is used to simply the creation of the objects of different classes that inherit from a common super-class (or interface X) depending on some condition.

The Recipe

In order to create a factory for a class X:

  1. Create an XFactory class
  2. Add an X createX(params...) method to it
  3. Depending on the value of the parameters create and return an object that is an instance of the appropriate subclass of X