Comparison
Strategy patternvsTemplate method
Strategy pattern
the class takes the algorithm as a constructor argument, and choosing a different one is a different argument.
Putting an interchangeable algorithm behind an interface so the object using it does not know which one it has. It is the pattern the caller chooses — a pricing rule, a sort comparator, a retry policy passed in from outside. In a language with first-class functions a strategy is usually just a function parameter, and saying so is a better answer than drawing the class diagram.
Full entry →Template method
the base class runs the five steps in order and leaves two of them abstract for you to write.
Defining the skeleton of an algorithm in a base class and deferring specific steps to subclasses. It is the inheritance-shaped sibling of strategy: same goal — vary part of an algorithm — with the variation bound at compile time by subclassing rather than at runtime by composition. It is the most common accidental producer of a fragile base class, because the ordering and the calls between steps become an unwritten contract.
Full entry →