Design Patterns: Factory Method (Virtual Constructor)

Posted by Monik, 08 October 2010.
Programming Code Design Design Patterns
Book summary
Summary on basis of the book ,,Design Patterns: Elements of Reusable Object-Oriented Software'', by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Addison-Wesley, 1995.

abstract class Creator{
abstract createProduct();
doSomething() {
...
Product p = createProduct();
p.blaBlaBla();
...
}
}
We use it when a class (Creator) needs to create and use an object (Product), but it does not need to know the type of the object.

We make this class abstract, and let its subclasses implement the method factoryMethod(), which will create and return the concrete object. The factoryMethod() method is then for example used in the abstract Creator whenever the Product of unknown type was needed.

We can also use this methd when we want to specify the object type to create in Creator subclasses.

Consequences:
Variations

Comments


Comments: