My teacher is a really good one and I tend to understand his points, but this one just goes over my head. He explai开发者_开发百科ns 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
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.
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:
- It is a behavioural design pattern
- 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.
- 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.
- superclass template method calls methods from subclasses,
Strategy pattern:
- It's a behavioural pattern
- It's based on delegation
- It changes guts of the object by modifying method behaviour
- It's used to switch between family of algorithms
- It changes the behaviour of the object at run time. One algorithm will be selected from a family of algorithm.
Basic differences.
- Template method uses Inheritance and Strategy uses composition
- 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
- 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.
精彩评论