What It Is
The Template Method pattern fixes the skeleton of an algorithm — the steps and their order — while letting the individual steps vary. The recipe is written once; each variant fills in its own steps.
In Python the skeleton lives in a real base class: abc.ABC. This is the one pattern where a base class beats a protocol, because the base must carry the shared skeleton method, not just describe a shape.
Example: One Recipe, Two Drinks
make() owns the order: boil, brew, pour, extras. Tea and coffee only answer how to brew and what to add — they cannot reorder or skip steps.
The skeleton stays fixed
Both drinks follow the same four steps in the same order, because the order lives in one method. A new drink implements two methods and inherits the whole discipline — and @abstractmethod refuses to even create a drink that forgot one.
Output: complete Python program
from abc import ABC, abstractmethod
class Drink(ABC):
# The fixed skeleton lives in the base class...
def make(self, name: str) -> None:
print("--", name)
print("boil water")
print(self.brew())
print("pour into cup")
print(self.extras())
# ...the variable steps are abstract methods.
@abstractmethod
def brew(self) -> str: ...
@abstractmethod
def extras(self) -> str: ...
class Tea(Drink):
def brew(self) -> str:
return "steep the tea leaves"
def extras(self) -> str:
return "add lemon"
class Coffee(Drink):
def brew(self) -> str:
return "run water through the coffee"
def extras(self) -> str:
return "add milk"
Tea().make("tea")
Coffee().make("coffee")
When to Use It
Use it when several procedures share the same fixed sequence with different details — report generation, import pipelines, test setups.
Skip it when the variants also need different orders — forcing them under one skeleton bends the pattern until it breaks. And with one variant, the skeleton is just a function.
Pros and Cons
Pros: the sequence is enforced in one place; variants cannot forget or shuffle steps; shared steps are written once.
Cons: rigid by design — a variant that needs "almost the same, but..." fights the skeleton; too many hook methods make the recipe hard to follow.