views:

280

answers:

3

Hi all. My teacher is a really good one and I tend to understand his points, but this one just goes over my head. He explains Template Method in two variants;
- Unification: the standard variant, that is composed of an abstract class with some abstract methods defining the variant parts of the otherwise fixed algorithm.
- Separation: his own variant (I think?) where a class contains the templateMethod() and uses delegation to an interface to vary the parts of the algorithm, which looks to me exactly like the Strategy pattern.

Can anyone see what his point is, and how the 'separation' variant is different from the Strategy pattern?
I have attached an image containing the two patterns from his book (which isn't published yet).

http://img64.imageshack.us/img64/3620/strategytemplate.jpg

+2  A: 

Try http://stackoverflow.com/questions/669271/what-is-the-difference-between-the-template-and-the-strategy-pattern

gavit
I read that, but that's about the 'unified' Template Method, which is not the same as the 'seperate' Template Method.
kTk
A: 

In common usage, Template method uses subclasses to provide the varied behaviours. With Strategy, you inject an algorithm object. In your example, there is no useful distinction between Template (separation) and Strategy. Given the age of the Gamma et al book, introducing this new terminology withou adequately explaining the difference is likely to simply cause confusion when you talk to other programmers. Avoid using it outside your lessons.

Template allows you to access protected members in the base class. Strategy allows you to develop you algorithms more losely coupled from the objects that use them, and allows you to inject the same algorithm into many different types of object.

Steve Cooper
You are right - but what I am asking about is the 'separating' variant of the Template Method. I fully agree with you on the 'unification' variant, but how do you understand the 'separation' one compared to Strategy? - there are no protected methods or abstract classes in this variant
kTk
I think your teacher has made up the 'separation' form, and there isn't a difference. If there is a difference, it's extremely fine and I don't think you need to worry about it. I'll update my answer accordingly.
Steve Cooper
I see, thanks for the answer.
kTk
+1  A: 

I have never heard of a "Separation variant" of the Template method pattern, and I agree that it looks extremely similar to the Strategy. Even if there is some reasoning about interface ownership or how you invoke them from a client perspective I hardly find there's any benefit to consider them different patterns.

waxwing
Well there must be a reason my teacher describes it the way he does. My impression is that we need to be able to explain this at the exam and I find it weird it there is no clear (not just subtle) difference. Could the difference be in how they are used? Like, Template Method is never invoked from a client, whereas Strategy is exactly meant to be invoked from a client so that he can change the Strategy? Does that make sense?
kTk