Factory Method Pattern

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:
Related: AbstractFactoryPattern, ClassFactory, DesignPatterns

CategoryPattern CategoryCreationalPatterns

View edit of March 7, 2008 or FindPage with title or text search