1

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

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
kTk
  • 13
  • 3
  • 1
    Do you have license from your teacher to publish the pages and discuss about them publicly? – kgiannakakis Jan 04 '10 at 09:05
  • Having read the question and the answers, I think I agree that this sounds like the Strategy pattern by another name. Have you asked your teacher about this? I'd be interested to hear what he says. – KarstenF Feb 20 '10 at 22:33

3 Answers3

1

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
  • 20,542
  • 15
  • 71
  • 88
  • 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 Jan 04 '10 at 09:52
  • 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 Jan 04 '10 at 11:54
1

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
  • 18,547
  • 8
  • 66
  • 82
  • 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 Jan 04 '10 at 09:56
0

Template Method:

  1. It is a behavioural design pattern
  2. it’s used to create a method stub and deferring some of the steps of implementation to the subclasses. It consists of certain steps whose order is fixed.
  3. It defines the steps to execute an algorithm and it can provide default implementation that might be common for all or some of the subclasses.
  4. superclass template method calls methods from subclasses,

Strategy pattern:

  1. It's a behavioural pattern
  2. It's based on delegation
  3. It changes guts of the object by modifying method behaviour
  4. It's used to switch between family of algorithms
  5. It changes the behaviour of the object at run time. One algorithm will be selected from a family of algorithm.

Basic differences.

  1. Template method uses Inheritance and Strategy uses composition
  2. The Template method implemented by the base class should not be overridden. In this way, the structure of the algorithm is controlled by the super class, and the details are implemented in the sub classes
  3. Strategy encapsulates the algorithm behind an interface, which provide us ability to change the algorithm at run time. Multiple strategies provide different implementation to interface.

Have a look at Journaldev Template method and Strategy articles for better understanding along with sourcemaking articles.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211