Origin: | GoF 95 | |||||||||||||||
Reason: | Provide an interface for creating an object, but let subclasses decide which class to instantiate (aka virtual constructor). | |||||||||||||||
Synopsis: | An abstract class, the FactoryMethod, defines abstract or near-empty methods that return product components and a final creator method to create an entire product. Subclasses override methods that return product components but use the same creator. | |||||||||||||||
Example: | An office productivity program manages two classes: Document, e.g., texts, images, sounds, etc., and Application to create or open documents. The Application class does not know which specific Document class to instantiate. | |||||||||||||||
Solution: |
|
|||||||||||||||
See also: |
AbstractFactory Builder Prototype creational pattern summary |
|||||||||||||||
Special: |
All the concrete product classes are known in advance. There is a variable or large number of product classes. |