Skip to content

Latest commit

 

History

History
69 lines (58 loc) · 1.36 KB

template-method.md

File metadata and controls

69 lines (58 loc) · 1.36 KB

Template Method Design Pattern

The Template Method Pattern defines the skeleton of an algorithm in a base class, allowing subclasses to override specific steps of the algorithm without changing its structure.

How to apply

abstract class BookProcessor
{
    public function __construct(protected string $title) {}

    public function process(): void
    {
        $this->start();
        $this->bind();
        $this->publish();
    }

    protected function start(): void
    {
        echo "Printing: {$this->title}";
    }

    abstract protected function bind(): void;

    protected function publish(): void
    {
        echo "Publishing: {$this->title}";
    }
}
class HardcoverProcessor extends BookProcessor
{
    protected function bind(): void
    {
        echo "Hardcover binding: {$this->title}";
    }
}
class PaperbackProcessor extends BookProcessor
{
    protected function bind(): void
    {
        echo "Paperback binding: {$this->title}";
    }
}

Usage:

$hardcoverProcessor = new HardcoverProcessor("The Hobbit");
$hardcoverProcessor->process();

$paperbackProcessor = new PaperbackProcessor("The Martian");
$paperbackProcessor->process();

Output:

Printing: The Hobbit
Hardcover binding: The Hobbit
Publishing: The Hobbit

Printing: The Martian
Paperback binding: The Martian
Publishing: The Martian