Template Method Pattern

noun

say it: “TEM-plit METH-ud PAT-urn” /ˈtɛmplɪt ˈmɛθəd ˈpætərn/

A design pattern in which a base class writes the steps of a routine once, in a fixed order, and leaves one or more steps for each subclass to fill in.

What is Template Method Pattern?

The Template Method pattern puts the fixed steps of a process in a base class and lets child classes fill in certain steps. Every report is "header, body, footer", but the body differs.

public abstract class Report
{
    public string Build() => Header() + Body() + Footer();
    protected string Header() => "ACME Corp\n";
    protected abstract string Body();
    protected string Footer() => "\n-- end --";
}

In a sentence

  • “With the Template Method pattern, the label's address step was fixed once for every carrier.”
  • “Test frameworks use the Template Method pattern: set up, your test, clean up, always in that order.”

Level: Master · More OOP terms at this level

Updated