What is the difference between the template method and the strategy patterns?
Can someone please explain to me what is the difference between the template method pattern and the strategy pattern is?
As far as I can tell they are 99% the same - the only difference being that the template method pattern has an abstract class as the base class whereas the strategy class uses an interface that is implemented by each concrete strategy class.
However, as far as the client is concerned they are consumed in exactly the same way - is this correct?
Solution 1:
The template pattern is used when a particular operation has some invariant behavior(s) that can be defined in terms of other varying primitive behaviors. The abstract class defines the invariant behavior(s), while the implementing classes defined the dependent methods.
In a strategy, the behavior implementations are independent -- each implementing class defines the behavior and there is no code shared between them. Both are behavioral patterns and, as such, are consumed in much the same way by clients. Typically strategies have a single public method -- the execute()
method, whereas templates may define a set of public methods as well as a set of supporting private primitives that subclasses must implement.
The two patterns could easily be used together. You might have a strategy pattern where several implementations belong to a family of strategies implemented using a template pattern.
Solution 2:
The main difference between the two is when the concrete algorithm is chosen.
With the Template method pattern this happens at compile-time by subclassing the template. Each subclass provides a different concrete algorithm by implementing the template's abstract methods. When a client invokes methods of the template's external interface the template calls its abstract methods (its internal interface) as required to invoke the algorithm.
class ConcreteAlgorithm : AbstractTemplate
{
void DoAlgorithm(int datum) {...}
}
class AbstractTemplate
{
void run(int datum) { DoAlgorithm(datum); }
virtual void DoAlgorithm() = 0; // abstract
}
In contrast, the Strategy pattern allows an algorithm to be chosen at runtime by containment. The concrete algorithms are implemented by separate classes or functions which are passed to the strategy as a parameter to its constructor or to a setter method. Which algorithm is chosen for this parameter can vary dynamically based on the program's state or inputs.
class ConcreteAlgorithm : IAlgorithm
{
void DoAlgorithm(int datum) {...}
}
class Strategy
{
Strategy(IAlgorithm algo) {...}
void run(int datum) { this->algo.DoAlgorithm(datum); }
}
In summary:
- Template method pattern: compile-time algorithm selection by subclassing
- Strategy pattern: run-time algorithm selection by containment
Solution 3:
I think the Class-Diagrams of both pattern are showing the differences.
Strategy
Encapsulates an algorithm inside a class
Link to image
Template Method
Defer the exact steps of an algorithm to a subclass
Link to Image