Design patterns: Template Method

date: · 3 min
author: Mauto
series: Design Patterns/ part 23/24

The classic Gang of Four patterns, one per part, with practical PHP examples.

  1. Design Patterns
  2. Design patterns: Singleton
  3. Design patterns: Factory Method
  4. Design patterns: Abstract Factory
  5. Design patterns: Builder
  6. Design patterns: Prototype
  7. Design patterns: Adapter
  8. Bridge Pattern in PHP: What It Is, When to Use It, with Examples
  9. Design patterns: Composite
  10. Design patterns: Decorator
  11. Design patterns: Facade, What It Is and How to Use It (PHP)
  12. Design patterns: Flyweight
  13. Design patterns: Proxy
  14. Design patterns: Chain of Responsibility Explained (PHP)
  15. Design patterns: Command
  16. Design patterns: Interpreter
  17. Design patterns: Iterator
  18. Design patterns: Mediator
  19. Design patterns: Memento
  20. Design patterns: Observer
  21. Design patterns: State, What It Is and How to Use It (PHP)
  22. Design patterns: Strategy
  23. Design patterns: Template Method
  24. Design patterns: Visitor
the whole guide >

The Template Method is a behavioral pattern that defines the skeleton of an algorithm inside a method, letting subclasses implement specific parts of the algorithm without changing its overall structure. This pattern lets you reuse code and promotes a clear separation between what is invariant and what varies within the algorithm.

Strengths

  1. Code reuse: The general structure of the algorithm is defined only once in the base class, promoting reuse of common code.
  2. Flexible algorithm definition: Subclasses can change only the specific parts of the algorithm, while the overall sequence stays the same.
  3. Preventing duplication: It reduces code duplication, because the common logic lives in a single base class.
  4. Control over the algorithm’s flow: The base class keeps control over the algorithm’s execution, making it hard for subclasses to alter the order of operations.
  5. Structural clarity: It allows a clear split between generic behavior (base class) and specific behavior (subclasses).

Use case: An online ordering process

Imagine a system for managing online orders. The ordering process follows some common steps: selecting the product, entering shipping details and processing the payment. However, the payment method can vary between credit card and PayPal.

PHP Code

// Abstract class that defines the template method
abstract class OrderProcessTemplate {
    // Template method that defines the algorithm (final: subclasses can't change its sequence)
    final public function processOrder() {
        $this->selectProduct();
        $this->addShippingDetails();
        $this->processPayment();
        $this->shipOrder();
    }

    // Common method for selecting the product, the same for all subclasses
    protected function selectProduct() {
        echo "Product selected.\n";
    }

    // Required method to be implemented in subclasses
    abstract protected function processPayment();

    // Final method for adding shipping details, invariant across subclasses
    final protected function addShippingDetails() {
        echo "Shipping details added.\n";
    }

    // Final method for shipping the order, invariant across subclasses
    final protected function shipOrder() {
        echo "Order shipped.\n";
    }
}

// Concrete implementation for an order paid by Credit Card
class CreditCardOrder extends OrderProcessTemplate {
    protected function processPayment() {
        echo "Payment made by Credit Card.\n";
    }
}

// Concrete implementation for an order paid with PayPal
class PayPalOrder extends OrderProcessTemplate {
    protected function processPayment() {
        echo "Payment made with PayPal.\n";
    }
}

// Usage example
$order1 = new CreditCardOrder();
$order1->processOrder();  // Ordering process with Credit Card

echo "\n";

$order2 = new PayPalOrder();
$order2->processOrder();  // Ordering process with PayPal

Explanation

  • OrderProcessTemplate class: Defines the Template Method processOrder(), declared final, which contains the sequence of operations common to all subclasses. The selectProduct() method is implemented only once in the base class, while processPayment() is declared abstract, leaving it to the subclasses to implement it.
  • Invariant methods: Methods such as addShippingDetails() and shipOrder() are common to all subclasses and, being declared final, cannot be changed.
  • Subclasses (CreditCardOrder, PayPalOrder): Implement only the specific part of the algorithm, namely the payment method.
  • Using the Template Method: The processOrder() method follows the predefined sequence, but the variable parts are delegated to the subclasses.

Conclusion

The Template Method Pattern lets you define the structure of an algorithm in the base class and delegate the implementation of specific details to subclasses. This approach makes the code more modular, reusable, and easy to extend.

BACK TO THE GUIDE

Copied