The FactoryMethodPattern is one of the DesignPatterns discussed in the GangOfFour book of the same name.
(Page 107)Be careful: AnOperation? method has to call FactoryMethod! The illustration above is confusing.
It allows classes to defer object creation to a separate method (a FactoryMethod). This would be an additional method on existing classes in a hierarchy.
Also called the VirtualConstructor; this is because in a statically-typed language (like CeePlusPlus) the object returned is of the type indicated by the method signature, or a subtype. Compare this with a standard constructor, which always gives you a Foo and never a subtype of Foo.
The factory method ensures an interface which returns the product type depending on the implementation of the creator class..It's similar to Abstract Factory in that the methods of the Abstract factory can be implemented as factory methods.The main difference is that while abstract factory deals with a family of products,the factory method is only worried about a single product.
I see that differently and agree with the description in AbstractFactoryVsFactoryMethod -- IljaPreuss?
Factory methods are sometimes used in place of constructors for any of several reasons:
Some languages (such as Java) do not allow constructors to have useful names
Some languages (such as Java) do not allow constructors to have different names (which may be necessary if you want to use the same method signature for two constructors)
To allow the same instance to be reused instead of recreated each time it is needed (see FlyweightPattern)