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
:
- Create an
XFactory
class - Add an
X createX(params...)
method to it - Depending on the value of the parameters create and return an object that is an instance of the appropriate subclass of
X