Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use template method Vs. Strategy?

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.

like image 269
dsimcha Avatar asked Mar 23 '09 02:03

dsimcha


People also ask

What are some differences between template method and strategy?

But, the key difference is that Strategy Pattern is about modifying a behaviour of a context in runtime using strategies, while Template Method Pattern is about following a skeleton implementation of an algorithm and modifying its behaviour by overriding methods of the skeleton class in the subclasses.

When a template method should be used?

The template method is used for the following reasons : Let subclasses implement varying behavior (through method overriding) Avoid duplication in the code, the general workflow structure is implemented once in the abstract class's algorithm, and necessary variations are implemented in the subclasses.

When should a developer use the strategy pattern?

Use the Strategy pattern when you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime.


2 Answers

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...

like image 74
Denis Troller Avatar answered Sep 21 '22 00:09

Denis Troller


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".

like image 36
Scott Stanchfield Avatar answered Sep 21 '22 00:09

Scott Stanchfield