The Template Method pattern is a behavioral design pattern that defines the skeleton of an algorithm in a base class but allows subclasses to override specific steps of the algorithm without changing its structure. It enables code reuse and provides a consistent way to define an algorithm’s structure while allowing customization in subclasses.

Key Characteristics:

  • Algorithm Skeleton: Defines the overall structure of an algorithm in a base class.
  • Customizable Steps: Allows subclasses to implement or override specific steps of the algorithm.
  • Code Reuse: Promotes code reuse by defining common behavior in the base class.

How It Works:

  1. Abstract Class: Defines a template method that contains the skeleton of the algorithm. It may include calls to abstract methods that subclasses need to implement.
  2. Concrete Subclasses: Provide specific implementations for the abstract methods and customize parts of the algorithm as needed.

Example in PHP:

  // Abstract Class
abstract class AbstractClass {
    // Template Method
    public function templateMethod() {
        $this->stepOne();
        $this->stepTwo();
        $this->stepThree();
    }

    // Concrete Methods
    protected function stepOne() {
        echo "Step One: Common Step\n";
    }

    // Abstract Methods
    abstract protected function stepTwo();

    protected function stepThree() {
        echo "Step Three: Common Step\n";
    }
}

// Concrete Subclass A
class ConcreteClassA extends AbstractClass {
    protected function stepTwo() {
        echo "Step Two: Implementation in ConcreteClassA\n";
    }
}

// Concrete Subclass B
class ConcreteClassB extends AbstractClass {
    protected function stepTwo() {
        echo "Step Two: Implementation in ConcreteClassB\n";
    }
}

// Client Code
$objectA = new ConcreteClassA();
$objectA->templateMethod(); 
// Output:
// Step One: Common Step
// Step Two: Implementation in ConcreteClassA
// Step Three: Common Step

$objectB = new ConcreteClassB();
$objectB->templateMethod(); 
// Output:
// Step One: Common Step
// Step Two: Implementation in ConcreteClassB
// Step Three: Common Step
  

Explanation:

  1. Abstract Class: AbstractClass defines the templateMethod() which outlines the structure of the algorithm. It includes both concrete methods (stepOne() and stepThree()) and abstract methods (stepTwo()).
  2. Concrete Subclasses: ConcreteClassA and ConcreteClassB implement the stepTwo() method, customizing the behavior of that step while inheriting the common steps from the base class.

Benefits:

  • Code Reuse: Promotes code reuse by defining common steps in the base class.
  • Consistency: Ensures a consistent algorithm structure across different subclasses.
  • Flexibility: Allows customization of specific steps in the algorithm without altering its overall structure.

Use Cases:

  • Algorithm Templates: When defining a consistent algorithm structure while allowing specific variations in the steps.
  • Frameworks: For creating frameworks where the overall process is defined but certain steps are left for subclasses to implement.
  • Workflow Management: When implementing workflows where the general process is fixed but certain steps need customization.

The Template Method pattern is useful for defining a consistent algorithm structure while allowing specific steps to be customized by subclasses, promoting code reuse, consistency, and flexibility in algorithm design.