PHP
Template method pattern
Learn to set up a method skeleton in the abstract class and let subclasses fill in steps.
The template method pattern
The template method pattern works like this: the parent abstract class defines the skeleton of a workflow in a concrete method. This method calls abstract parts that subclasses fill in. It is called the template method.
For example, a report is built as: header + body + footer. The parent's build() method fixes this order; header() and body() are abstract, implemented by the subclass. footer() can be concrete; the subclass may override it.
This pattern reduces duplication and keeps the workflow in one place.
Hook methods: optional extension
Sometimes the parent tells the subclass 'you can skip or change this step'. This is a hook method. A hook is concrete (has a body) in the parent; usually it returns nothing or a default value. The subclass may override it, or use the default.
In the previous report example, footer() is a hook: by default it returns "---\n"; HtmlReport may override it with its own footer.
Unlike an abstract method, a hook is an optional extension point. It does not break the template method's skeleton.
Exercise Template method: Report system
Define a Report abstract class. Declare two abstract methods: header(): string and body(): string. Add a concrete method footer() that returns a default: return "---\n";. The template method public function build(): string should do: return $this->header() . $this->body() . $this->footer();. Then derive HtmlReport from Report and implement header() and body(). Use $r = new HtmlReport() and echo $r->build();
Show example solution
In the Report abstract class declare abstract header() and body(). The concrete footer() returns "---\n". build() combines header+body+footer. The HtmlReport subclass implements header() and body().
Example solution:
<?php
// Define the Report abstract class
abstract class Report {
abstract public function header(): string;
abstract public function body(): string;
public function footer(): string {
return "---\n";
}
public function build(): string {
return $this->header() . $this->body() . $this->footer();
}
}
class HtmlReport extends Report {
public function header(): string {
return "<h1>Title</h1>";
}
public function body(): string {
return "<p>Body</p>";
}
}
$r = new HtmlReport();
echo $r->build();
Test yourself
-
What is the 'template method' in the template method pattern?
- An abstract method written by the subclass
- A method that is only static
- A private hidden method
- A concrete method in the parent that calls abstract parts
Show answer
A concrete method in the parent that calls abstract parts — Correct. The template method is concrete in the parent and calls the abstract parts filled by subclasses.
-
Complete the template method
public function build(): string {
return ____ . $this->body() . $this->footer();
}
Show answer
public function build(): string {
return $this->header() . $this->body() . $this->footer();
}
-
Match the template method concepts
- template method
- Sets the skeleton
- abstract part
- Implemented in subclass
- hook method
- Has default body
- concrete method
- Body is complete
Next lesson: Constructor and abstract