User Tools

Site Tools


php:template

TEMPLATE METHOD PATTERN

14.04.2010

 Template Pattern

A template method defines the program skeleton of an algorithm. One or more of the algorithm steps are able to be overridden by subclasses to provide their own concrete implementation. This allows differing behaviors while ensuring that the overarching algorithm is still followed.

Subclasses change the abstract methods to implement real actions. Thus the general algorithm is saved in one place but the concrete steps may be changed by the subclasses.

Abstract and Concrete Classes

<?php
/**
 * The AbstractClass.
 */
abstract class BinaryOperation
{
    /**
     * These are three hooks defined, which should
     * provide the two numbers which the operation is
     * applied to and its business logic.
     */
    protected abstract function _getFirstNumber();
    protected abstract function _getSecondNumber();
    protected abstract function _operator($a, $b);
 
    /**
     * This is the Template Method.
     * It uses all the three hooks, but a typical
     * Template Method can coexist with other ones, and
     * share hooks with them.
     * @return numeric
     */
    public function getOperationResult()
    {
        $a = $this->_getFirstNumber();
        $b = $this->_getSecondNumber();
        return $this->_operator($a, $b);
    }
}
 
/**
 * A ConcreteClass.
 */
class Sum extends BinaryOperation
{
    private $_a;
    private $_b;
 
    public function __construct($a = 0, $b = 0)
    {
        $this->_a = $a;
        $this->_b = $b;
    }
 
    protected function _getFirstNumber()
    {
        return $this->_a;
    }
 
    protected function _getSecondNumber()
    {
        return $this->_b;
    }
 
    protected function _operator($a, $b)
    {
        return $a + $b;
    }
}
 
/**
 * A ConcreteClass.
 */
class NonNegativeSubtraction extends BinaryOperation
{
    private $_a;
    private $_b;
 
    public function __construct($a = 0, $b = 0)
    {
        $this->_a = $a;
        $this->_b = $b;
    }
 
    protected function _getFirstNumber()
    {
        return $this->_a;
    }
 
    protected function _getSecondNumber()
    {
        return min($this->_a, $this->_b);
    }
 
    protected function _operator($a, $b)
    {
        return $a - $b;
    }
 
}
 
// Client code
$sum = new Sum(84, 56);
echo $sum->getOperationResult(), "\n";
$nonNegativeSubtraction = new NonNegativeSubtraction(9, 14);
echo $nonNegativeSubtraction->getOperationResult(), "\n";

Credits: Giorgio Sironi

php/template.txt · Last modified: 2013/03/16 17:40 (external edit)