Template Method and Strategy design patterns
Asked Answered
T

2

8

This is probably a newbie question since I'm new to design patterns but I was looking at the Template Method and Strategy DP's and they seem very similar. I can read the definitions, examine the UML's and check out code examples but to me it seem like the Strategy pattern is just using the Template Method pattern but you just happen to passing it into and object (i.e. composition).

And for that matter the Template Method seems like that is just basic OO inheritance.

Am I missing some key aspect to their differences? Am I missing something about the Template Method that makes it more that just basic inheritance?

Note: There is a previous post on this (672083) but its more on when to use it, which kind of helps me get it a bit more but I want valid my thoughts on the patterns themselves.

To answered 12/6, 2009 at 20:43 Comment(0)
T
10

It basically all comes down to semantics. The strategy pattern allows you to pass in a particular algorithm/procedure (the strategy) to another object and that will use it. The template method allows you to override particular aspects of an algorithm while still keeping certain aspects of it the same (keep the order the same, and have things that are always done at the start and end for example... the 'template') while inheritance is a way of modelling 'IS-A' relationships in data models.

Certainly, template methods are most easily implemented using inheritance (although you could just as easily use composition, especially once you have functors), and strategy patterns are frequently also template methods but where the syntax is similar the meanings are vastly different.

Thiosinamine answered 12/6, 2009 at 20:50 Comment(3)
+1 for a good concise explanation, and for mentioning composition over inheritanceZagreus
The Template Method pattern employs inheritance by definition. It cannot be implemented by composition. "Template Method lets subclasses redefine certain steps of an algorithm..." The first two sentences of the chapter both state that the pattern is implemented by subclasses (page 325).Scion
Conversely, the Strategy pattern employs composition by definition. It cannot be implemented by inheritance. Indeed, Strategy is "An alternative to subclassing." (page 317).Scion
N
1

The Strategy design pattern
provides a way to exchange the algorithm of an object dynamically at run-time
(via object composition).

For example, calculating prices in an order processing system.
To calculate prices in different ways, different pricing algorithms can be supported so that which algorithm to use can be selected (injected) and exchanged dynamically at run-time.

The Template Method design pattern
provides a way to redefine some parts of the behavior of a class statically at compile-time
(via subclassing).

For example, designing reusable applications (frameworks).
The application implements the common (invariant) parts of the behavior so that users of the application can write subclasses to redefine the variant parts to suit their needs. But subclass writers should neither be able to change the invariant parts of the behavior nor the behavior's structure (the structure of invariant and variant parts).

Neva answered 21/2, 2015 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.