mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-28 10:40:17 +02:00
All public methods of abstract classes should be final. Enforce API encapsulation in an inheritance architecture. If you want to override a method, use the Template method pattern.
66 lines
1.5 KiB
PHP
66 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Behavioral\TemplateMethod;
|
|
|
|
abstract class Journey
|
|
{
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
private array $thingsToDo = [];
|
|
|
|
/**
|
|
* This is the public service provided by this class and its subclasses.
|
|
* Notice it is final to "freeze" the global behavior of algorithm.
|
|
* If you want to override this contract, make an interface with only takeATrip()
|
|
* and subclass it.
|
|
*/
|
|
final public function takeATrip()
|
|
{
|
|
$this->thingsToDo[] = $this->buyAFlight();
|
|
$this->thingsToDo[] = $this->takePlane();
|
|
$this->thingsToDo[] = $this->enjoyVacation();
|
|
$buyGift = $this->buyGift();
|
|
|
|
if ($buyGift !== null) {
|
|
$this->thingsToDo[] = $buyGift;
|
|
}
|
|
|
|
$this->thingsToDo[] = $this->takePlane();
|
|
}
|
|
|
|
/**
|
|
* This method must be implemented, this is the key-feature of this pattern.
|
|
*/
|
|
abstract protected function enjoyVacation(): string;
|
|
|
|
/**
|
|
* This method is also part of the algorithm but it is optional.
|
|
* You can override it only if you need to
|
|
*/
|
|
protected function buyGift(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
private function buyAFlight(): string
|
|
{
|
|
return 'Buy a flight ticket';
|
|
}
|
|
|
|
private function takePlane(): string
|
|
{
|
|
return 'Taking the plane';
|
|
}
|
|
|
|
/**
|
|
* @return string[]
|
|
*/
|
|
final public function getThingsToDo(): array
|
|
{
|
|
return $this->thingsToDo;
|
|
}
|
|
}
|