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.
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}";
}
}
$hardcoverProcessor = new HardcoverProcessor("The Hobbit");
$hardcoverProcessor->process();
$paperbackProcessor = new PaperbackProcessor("The Martian");
$paperbackProcessor->process();
Printing: The Hobbit
Hardcover binding: The Hobbit
Publishing: The Hobbit
Printing: The Martian
Paperback binding: The Martian
Publishing: The Martian