Factory Method mode
Alias
Virtual constructor
structure
advantage
1. Creator does not know the class name of the object you need to create, which will be responsible for this decision by the derived class. This brings great flexibility to object creation. See AbstractFactory mode, creating a product is usually using FactoryMothoD mode.
2, specific relationships between subclasses, determined by subclasses their own (Localizes Knowledge of Which Classes Belong Together). Examples of the reference book:
Disadvantages As discussed in AbstractFactory mode, each CONCRETEPRODUCT requires a CONCRETECREATOR corresponding to. It may result in a lot of classes that need to be derived.
The solution is:
1, use templates (Template)
2, replace the Factory Method mode with prototype mode. However, Producting in Prototype typically requires an initialize () function, and Factory Method mode does not need, because this is initialization to pass to FactoryMethod as a parameter; prototype mode is not, because each prototype is different, can not specify The parameter form of Clone.
Reference "Design Pattern" Gang-Of-4 1997
Jady Leung
October 6, 2004