Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top