Template Method Pattern

Intent: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. TemplateMethod lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. DesignPatterns, p. 325

Template method pattern could be refactored using an interface that explicitly signals the methods requested to subclasses and also the state needed by them from the abstract class. Concrete implementations of the logic do not need any more to be subclasses of the class defining the skeleton algorithm. When using an abstract class, the dependencies between the concrete implementation and the base class are expressed by the protected interface of the abstract class (because ClassesHaveInterfaces) and this sometimes might be unnecessary, especially because of availability of public methods. When using an interface, the dependencies between the two classes are limited to the defined interface. Anybody agrees?

-- MicheleVivoda?

This is actually a good idea with one exception. If your sub methods have to share data, as they are wont to do, you'd have to pass around a lot of parameters (interfaces can't define instance variables). By using an AbstractClass for the template, you avoid all that nasty parameter passing.

-- MichaelDavidBrown?

It might be impractical in some cases to make explicit all the services and state needed from a subclass on an interface but this is only an ideal separation of dependencies that one should be aware of, then it can be applied only where it is really needed. For the problem of passing a lot of parameters, this might be a signal that a new entity should be created in the system, a class or maybe even an interface should be defined for it and passed around instead of parameters; this new interface could then also be implemented by the same sub class instance at the begining but the relevant fact is to recognize that this class/interface/entity might exist separately from the base class instead of leaving it hidden in some protected fields of the base class that are then read and possibly modified at runtime by a sub class. Identifying and isolating an interface makes it possible at a later stage to change its implementation or (even more interestingly) to allow different implementations to be plugged in; documenting an interface is a step further in allowing yourself and other people to understand how the system is working. But, of course, any abstraction ends at a certain point and it is not possible/practical to find an interface for everything; deciding what your abstraction should define and where to stop (or to get to) is probably the harder task.

-- MicheleVivoda?

If the TemplateMethodPattern is stubbornly or hastily applied without a thorough understanding of the algorithm's steps, this results in the AntiTemplateMethod?, which is one of the more effective ways of implementing BigBallOfMud.

Se puede utilizar en la construcción de frameworks, definiendo una estructura genérica, en la que las subclases podrán definir la implementación de los algoritmos. La elección de las clases que implementará los algoritmos del template method podrá ser realizado con una Factory (Factory Method).

(Translation: This can be used to construct frameworks, defining an abstract class where the subclasses can provide the implementation of the algorithms. You can then use a Factory to select which implementation of the template is used.)

See also NonVirtualCallsPureVirtualIdiom
The above comment, "This can be used to construct frameworks..where the subclasses can provide the implementation of the algorithms",actually describes strategy pattern, which is very similar to template pattern But the intent is different.Strategy is used to allow different implementations of an algorithm, or operation, to be selected dynamically at run time.The client is generally aware of the different implementations and is able to choose them.

The intention of the Template pattern is not to allow behavior to be implemented in different ways, as in Strategy, but rather to ensure that certain behaviors are implemented. In other words, where the focus of Strategy is to allow variety, the focus of Template is to enforce consistency.


CategoryPattern CategoryBehavioralPatterns

View edit of September 2, 2011 or FindPage with title or text search