SteelReed

Search

Search posts, questions…

OOP & DesignQuestion 25 / 42

Junior

What is Template Method, how does it differ from Strategy, and what is a hook method?

Short answer

Template Method: the parent keeps a fixed algorithm skeleton in a final method and subclasses fill in the steps. Strategy: swap the whole algorithm for another object at runtime.

swapped at runtimeprocess: finalfixed skeletonAbstract step:subclass must implementHook: has a default,subclass may overrideStrategyThe whole algorithm

3 points to say:

  1. A final skeleton keeps the order fixed.
  2. A hook is an optional step with a default.
  3. Strategy swaps the whole algorithm at runtime.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.