← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Template Method Pattern

quality Intermediate
debt(d7/e5/b5/t3)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints note automated=no, and while phpstan is listed as a tool, it cannot reliably detect misapplication of the Template Method pattern (e.g. making steps public, having too many abstract steps, or missing documentation). These issues surface only during code review when a reviewer notices the structural problem — duplicated algorithm skeletons or incorrectly scoped methods.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix describes defining an abstract base class with abstract steps, but common mistakes such as too many abstract steps, wrong visibility, or choosing Template Method over Strategy each require revisiting the class hierarchy across multiple concrete subclasses. This is more than a one-line swap but typically contained within one component or feature area.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). Template Method is inheritance-based and applies broadly across web, cli, and queue-worker contexts. Every subclass author must read and understand the base class to know the call order and what to override, imposing an ongoing comprehension burden. It doesn't define the whole system's shape, but any algorithm governed by this pattern imposes a persistent structural constraint on all subclass authors.

t3 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'minor surprise (one edge case)' (t3). The misconception field clarifies the main trap — developers assume deep hierarchies are needed, when only one level is required. The common mistakes (public steps, too many abstract steps, missing documentation) are well-known OOP gotchas rather than catastrophic wrong-guesses. A competent OOP developer will broadly understand the pattern but may misjudge scope or visibility on first use.

About DEBT scoring →

Also Known As

template method pattern Hollywood principle pattern

TL;DR

Defines the skeleton of an algorithm in a base class, deferring specific steps to subclasses without changing the algorithm's structure.

Explanation

The Template Method pattern places an algorithm's invariant steps in a base class method, while hook or abstract methods allow subclasses to provide varying implementations of specific steps. For example, a ReportGenerator base class defines generate() which calls fetchData(), formatRows(), and renderHeader() in sequence — subclasses override only the steps that differ. This enforces the algorithm's structure while permitting extension at defined points. In PHP it naturally uses abstract classes. Prefer Strategy over Template Method when the varying behaviour should be composable or swappable at runtime.

Common Misconception

Template method requires deep inheritance hierarchies. It needs only one level — a base class defines the algorithm skeleton with abstract steps; concrete subclasses implement just those steps. It is one of the simplest patterns to apply incrementally.

Why It Matters

The Template Method pattern defines the skeleton of an algorithm in a base class and lets subclasses fill in specific steps — it enforces a consistent structure while allowing variation in the details.

Common Mistakes

  • Making template steps public — they should be protected so only the template method calls them.
  • Too many abstract steps — if every step is abstract, the base class provides no structure.
  • Not documenting the template method — subclass authors must read the base class to understand the call order.
  • Using Template Method when Strategy would allow runtime behaviour switching without subclassing.

Code Examples

✗ Vulnerable
// Duplicated algorithm skeleton in two classes:
class CsvExporter {
    public function export(): void { $this->open(); $this->writeHeader(); $this->writeRows(); $this->close(); }
}
class XmlExporter {
    public function export(): void { $this->open(); $this->writeHeader(); $this->writeRows(); $this->close(); }
}
// The sequence is identical — extract to a template method in base class
✓ Fixed
abstract class DataImporter {
    // Template method — defines the algorithm skeleton
    final public function import(string $source): int {
        $raw   = $this->read($source);   // step 1 — abstract
        $rows  = $this->parse($raw);     // step 2 — abstract
        $valid = $this->validate($rows); // step 3 — hook with default
        return $this->save($valid);      // step 4 — abstract
    }

    abstract protected function read(string $source): string;
    abstract protected function parse(string $raw): array;
    abstract protected function save(array $rows): int;

    protected function validate(array $rows): array {
        return $rows; // default: no-op; subclasses can override
    }
}

class CsvImporter  extends DataImporter { ... }
class JsonImporter extends DataImporter { ... }

Added 15 Mar 2026
Edited 22 Mar 2026
Views 23
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 4 Unknown AI 3 Ahrefs 2 ChatGPT 2 SEMrush 2 Google 1
crawler 20 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Define the algorithm skeleton in an abstract base class with abstract steps — subclasses implement the steps without changing the algorithm structure; perfect for report generation, data import pipelines
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Same algorithm structure duplicated across multiple classes with different implementations of certain steps; base class calling abstract methods subclasses override
Auto-detectable: ✗ No phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant