views:

1110

answers:

6

The template method pattern and the strategy pattern do roughly the same thing. I understand the basic differences between them (template method is inheritance based, strategy is composition based), but are there any decent guidelines on when to choose one over the other? It seems like they do basically the same thing.

+1  A: 

One of the central OO Design principles is "Favour Composition over Inheritance", so that suggests to favour the Strategy pattern. It obviously depends on what you are trying to accomplish in a particular scenario.

Mitch Wheat
+9  A: 

Strategy allows for a reusable algorithm to be used in more than one place. If you have an algorithm that can be provided by your consumer and can be used in several places, this is a good spot for Strategy (sorting algorithms, predicates, comparers... are good examples of that).

Template method is specifically targeted at cases where you want people to be able to inherit from your class and want them to be able to override your implementation in a controlled manner (basically preventing them from replacing all your plumbing and offering them a specific extension point without risking a problem because they did not call the base method or called it at the wrong time).

They can be similar, and they can serve the same kind of purpose depending on what you are actually doing. As with all design patterns, it is difficult to answer such a question because there is not really a definitive answer. It's actually easier to decide in context...

Denis Troller
+3  A: 

You can create big inheritance tree just to change one of the N behavior. And you can create second big inheritance tree to change second of the N behavior.

But also you can unload your tree by creating small strategy trees.

So if you noticed that you add more and more classes just to add some changes in some behavior - it is time to supply your classes with strategies.

Mykola Golubyev
+5  A: 

Consider usage strategy when:

  • Your object behaviour needs to be changed in runtime.
  • You already have class hierarchy by other criteria.
  • You want to share strategy logic across different classes.

In other cases it should be enought to use template pattern.

Andrey Vityuk
is that 'when all of these' or 'when either of these'?
xtofl
surely "when any of these is true."
Steven Huwig
+2  A: 

I use Template method when the algorithm needs knowledge of the internals of the objects it runs on.

In all other cases (i.e. when the algorithm only needs to use the object's interface), I try to use Strategy.

Further, Strategy is only useful when there are actual algorithms to implement: If the only difference between classes is (for example) what simple value to return, use Template method.

Lennaert
+1  A: 

The two can actually be used together quite effectively.

Don't think of patterns as recipes with specific code to implement them.

It's the design intent that is the key, and there can be many implementations. By mentioning a pattern name in your code somewhere, you're letting a reader in on your intent when you wrote that code. The implementation is secondary.

Template method gives you an "algorithm with replaceable steps". (The algorithm is normally defined in a non-overridable method (final or private for example) )

The GoF implementation of this concept uses inheritance and method overriding to replace those steps.

However, you're still using Template method if those steps are replaced by strategies.

For example, think about a class that wants to walk a binary tree inorder and "do something" at each node.

The intent is that the inorder() method is a template method - the structure of the walk is always the same.

The "hook" method, the part that "does something" can be implemented as a method in the same class (and overridden in subclasses to change behavior), or externally, in which case it's a strategy for "doing something".

Scott Stanchfield