How does the Template Method pattern differ from the Strategy pattern?

The Template Method pattern fixes an algorithm's overall structure in a base class and lets subclasses override individual steps through inheritance, while the Strategy pattern extracts the entire algorithm into a separate, swappable object used through composition.

Key Points: • Template Method varies parts of an algorithm via subclassing; Strategy varies the whole algorithm via composition. • Template Method's structure is fixed at compile time by class hierarchy; Strategy can be swapped at runtime by injecting a different strategy object. • Template Method typically has a "hook" method structure inside one class hierarchy; Strategy has fully independent, interchangeable implementations of a common interface. • Strategy avoids inheritance altogether, favoring "composition over inheritance." • Choose Template Method when steps share a common skeleton; choose Strategy when entire algorithms need to be interchangeable.

Example: A report generator using Template Method would fix the steps of fetch-transform-render in a base class and let subclasses override just the transform step, whereas a Strategy-based sorter would let you inject a completely different Comparator implementation at runtime without any shared base algorithm.

Interview Tip: A concise interview answer is:

"Template Method uses inheritance to let subclasses override specific steps of an otherwise fixed algorithm, while Strategy uses composition to swap out an entire algorithm at runtime as an interchangeable object — Template Method fits shared-skeleton processes, Strategy fits fully interchangeable behaviors."