kumikill
5/10/2018 - 11:47 PM

Factory Method

Source: http://www.vincehuston.org/dp/factory_method.html

Intent

  • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
  • Defining a "virtual constructor".
  • The new operator is considered harmful.

Problem

A framework needs to standardize the architectural model for a range of applications, but allow for individual applications to define their own objects and provide for their instantiation.

Example

The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate. Injection molding presses demonstrate this pattern. Manufacturers of plastic toys process plastic molding powder, and inject the plastic into molds of the desired shapes. The class of toy (car, action figure, etc.) is determined by the mold.