design patterns · medium
Template Method Pattern
Template Method is a behavioral GoF pattern that defines the skeleton of an algorithm in a base-class method, deferring some steps to subclasses so they can redefine those steps without changing the algorithm's overall structure. The template method (e.g. DataMiner::mine) fixes the invariant order — open, parse, analyse, optionally report — and is ideally non-virtual/final so subclasses cannot reorder or skip steps. It is built from three parts: the template method itself; primitive operations (pure-virtual steps a subclass MUST implement, like openFile/parse); and hooks (virtual steps with a default the subclass MAY override, like wantsReport). Subclasses write only what differs while shared steps and the ordering live once in the base, eliminating duplicated algorithm skeletons. It embodies the Hollywood Principle ('don't call us, we'll call you') and the C++ non-virtual-interface idiom. Its cost is reliance on inheritance: variation is bound at compile time, one variant per subclass, with fragile-base-class risk and no runtime step swapping. The key contrast is Strategy, which varies the whole algorithm via composition and can swap it at runtime, whereas Template Method varies individual steps via inheritance.
Template Method (behavioral): define an algorithm's skeleton in a base-class method (ideally final) and defer the variable steps to subclasses — pure-virtual primitive operations they MUST implement and virtual hooks they MAY override. The base owns the step order (Hollywood Principle: it calls down into the subclass). Dedups the shared algorithm but is inheritance-bound/compile-time; Strategy is the composition-based runtime cousin that swaps the whole algorithm.
The code
// Template Method — fix the ALGORITHM's skeleton in a base class,// let subclasses fill in the variable steps (the 'hooks').class DataMiner {public: // The template method: the invariant sequence. NON-virtual + final // so subclasses cannot reorder or skip the steps. void mine(const std::string& path) final { auto raw = openFile(path); // step varies auto data = parse(raw); // step varies analyse(data); // step fixed (shared) if (wantsReport()) report(); // optional HOOK, default below } virtual ~DataMiner() = default;
protected: virtual std::string openFile(const std::string&) = 0; // required virtual Rows parse(const std::string&) = 0; // required void analyse(const Rows&) { /* shared logic */ } // invariant virtual bool wantsReport() { return true; } // hook: default void report() { /* shared logic */ }};
struct CsvMiner : DataMiner { // fills in only the holes std::string openFile(const std::string& p) override { /*...*/ } Rows parse(const std::string& r) override { /*...split on ','*/ }};What this lesson walks through
- 01Intent — fix the algorithm's skeleton, vary steps
- 02Structure — template method + primitive ops + hooks
- 03Subclasses fill in only the varying steps
- 04Gotcha — fragile base class + hook discipline
Template Method defines the SKELETON of an algorithm in a base-class method, deferring some steps to subclasses. The overall sequence is fixed and lives in one place; subclasses customize only the steps that differ.
See it animated — step by step, at your own pace
Unlock the full interactive walkthrough of Template Method Pattern and 100+ animated C++ interview lessons.