Tuesday, April 17, 2007

Factory Method and Template Method

When the sematics requires a procedure instantiate an object from a certain class/interface while the latter has to be instantiated in a specific concrete subclass, it is desirable to leave a pure virtual function in the interface of the former for creation of the object. This interface function must be implemented in the subclasses to create necessary concrete objects needed by the abstract procedure that calls it. Another paraphrase of the function is that it acts as a hook which must be filled in subclasses.

As you might guess, there will be as many subclasses of Creator as those of Product. However, the templates in C++ helps in this case by automating the parallel hiarachies.

The abstract procedure that calls Factory Methods are usually called Template Method, which is illustrated in the following figure:

No comments: