'Using Template Method and Strategy together

Gang of Four sums up the difference between Template Method and Strategy as follows:

Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.

What are the scenarios where combining two designs would be justified and how their relationship would look like?

Trivial example would involve delegating to Strategy inside hook methods of a Template Method, however, I cannot think of a good justification for such design. Additionally, instead of delegating to subclass one might delegate to Strategy directly. But then, without inheritance, we cannot talk about Template Method at all.



Solution 1:[1]

I think that it's perfectly possible to combine both patterns.

You use the Strategy pattern when you want to change a certain behavior at run time, simply changing the instance of the strategy.

enter image description here

I.e., you can change the behavior of the instances of "Context" by assigning "strategy" to a different concrete Strategy class. It provides the same functionality than having a field that you can change and doing a chain of if/elses or a switch based on the content of this field. The strategy pattern simply a more sophisticated way to do conditional. The advantage of using the strategy over a hard coded conditional is that you can put more conditions by adding new classes, without having to modify existent ones (the "O" in the "SOLID" principle).

The template method, by other hand, defines an algorithm that has some "hooks" left open, that must be overwritten by concrete class.

enter image description here

Both patterns are concerned about an algorithm by two different point of views: strategy about the ability of exchanging an algorithm at runtime, and template method about flexibilizing the structure of an algorithm. I don't see why you can't combine both. For me it's perfectly possible to have a strategy that is defined as a template method.

Solution 2:[2]

I agree with Danilo, although I differ a bit in the explanation.

I am currently trying to build a WordFinder Generator program and I want to provide different implementations of how to place words in the grid (e.g. finding all solutions with backtracking or using an optimistic random approach that eventually will find a solution). For these implementations I believe that a strategy pattern is the one.

Furthermore I want to be more flexible with the implementation of the algorith. There are parts that could be common (clear the grid), and others could be implemented differently (when a solution is valid, skipping words that cannot be placed...). These variations of the general strategy (pseudocode):

public final CreateBoard() {
    ClearBoard();
    FindSolution(); --override
    FillGaps();
}

I think that this could be perfectly implemented by means of the Template pattern. Clear and fill the gaps of the board would not be duplicated using this approach, and the strategy is limited to implement different versions of the FindSolution method.

Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Daniel Fernández