Template Method Design Pattern in PHP

1. Definition

The Template Method Design Pattern defines the program skeleton of an algorithm in a method but delays some steps to subclasses. It allows subclasses to redefine certain steps of an algorithm without changing the algorithm's structure.

2. Problem Statement

Suppose you're building a system that generates reports in various formats, such as HTML and XML. While the structure of generating these reports is the same (get data, format data, display report), the specific way data is formatted differs based on the report type.

3. Solution

The Template Method Pattern suggests moving the invariant parts of the logic into a base class and then having subclasses provide the specific implementations for the steps that can vary. The steps to be implemented by subclasses are typically made abstract in the base class.

4. Real-World Use Cases

1. Data processors that fetch, process, and save data. Fetching and saving are common steps, but processing can vary.

2. E-commerce order processing where the steps are to select product, make payment, and deliver. The delivery method might vary (e.g., digital download vs. courier delivery).

5. Implementation Steps

1. Identify the steps of the algorithm that are invariant and those that can vary.

2. Create an abstract base class with the template method that outlines the algorithm's skeleton.

3. Declare abstract methods for the varying steps.

4. Create concrete subclasses that implement the abstract methods.

6. Implementation in PHP

// Abstract class with the template method
abstract class ReportGenerator {
    // The template method
    public function generateReport() {
        $data = $this->fetchData();
        $formattedData = $this->formatData($data);
    abstract protected function fetchData();
    abstract protected function formatData($data);
    protected function displayReport($formattedData) {
        echo $formattedData;
// Concrete class for HTML report generation
class HTMLReportGenerator extends ReportGenerator {
    protected function fetchData() {
        return "Sample Data for HTML Report";
    protected function formatData($data) {
        return "<div>" . $data . "</div>";
// Concrete class for XML report generation
class XMLReportGenerator extends ReportGenerator {
    protected function fetchData() {
        return "Sample Data for XML Report";
    protected function formatData($data) {
        return "<data>" . $data . "</data>";
// Client Code
$report = new HTMLReportGenerator();
echo "\n";
$report = new XMLReportGenerator();


Sample Data for HTML Report
Sample Data for XML Report


1. ReportGenerator is an abstract class that defines a template method named generateReport. This method outlines the steps to generate a report.

2. Steps like fetchData and formatData are made abstract, as their implementation can vary.

3. We've created two concrete implementations: HTMLReportGenerator and XMLReportGenerator to generate HTML and XML reports, respectively.

4. In the client code, we've generated reports for both HTML and XML types.

7. When to use?

Use the Template Method Pattern when:

1. You have an algorithm that has common steps, but some steps might vary.

2. You want to avoid code duplication, keeping the structure of an algorithm consistent across different classes.

3. You want to provide hooks (abstract or default methods in the template method) for subclasses to easily extend specific steps of an algorithm.