Design Patterns PHP - Template method Java

1) What have we done ?

We just created abstract class "AbstractClass" and implemented there whole algorithm.


abstract class AbstractClass
{
    /**
     * @param boolean $doStep2
     */
    public function doSomething($doStep2)
    {
        $this->doStep1();
        if ($doStep2)
            $this->doStep2();
        $this->doStep3();
    }

    abstract protected function doStep1();

    abstract protected function doStep2();

    abstract protected function doStep3();
}

class ClassA extends AbstractClass
{
    protected function doStep1()
    {
        echo 'ClassA doing step one' . PHP_EOL;
    }

    protected function doStep2()
    {
        echo 'ClassA doing step two' . PHP_EOL;
    }

    protected function doStep3()
    {
        echo 'ClassA doing step three' . PHP_EOL;
    }
}

class ClassB extends ClassA
{
    protected function doStep1()
    {
        echo 'ClassB doing step one' . PHP_EOL;
    }

    protected function doStep2()
    {
        echo 'ClassB doing step two' . PHP_EOL;
    }

    protected function doStep3()
    {
        echo 'ClassB doing step three' . PHP_EOL;
    }
}




In subclasses we implement particular steps now we don't have any duplication.