Builder パターン | デザインパターン
2022年9年25日
デザインパターン
Builder
PHP
オライリージャパンによる Head First シリーズ デザインパターン(第2版)第14章 Builder パターンを、 PHPで書き直してみようという試みです。
AbstractBuilder.php
namespace Builder; interface AbstractBuilder { public function buildDay(string $day) : void; public function addHotel(string $hotel) : void; public function addReservation(string $reservation) : void; public function addSpecialEvent(string $specialEvent) : void; public function addTickents(string $tickets) : void; public function getVacationPlanner() : Planner; }
VacationBuilder.php
namespace Builder; class VacationBuilder implements AbstractBuilder { protected Planner $vacation; public function __construct() { $this->vacation = new Planner(); } public function buildDay(string $day) : void { $this->vacation->setBuildDay($day); } public function addHotel(string $hotel) : void { $this->vacation->setHotel($hotel); } public function addReservation(string $reservation) : void { $this->vacation->setReservation($reservation); } public function addSpecialEvent(string $specialEvent) : void { $this->vacation->setSpecialEvent($specialEvent); } public function addTickents(string $tickets) : void { $this->vacation->setTickents($tickets); } public function getVacationPlanner() : Planner { return $this->vacation; } }
Planner.php
namespace Builder; class Planner { public ?string $day = null; public ?string $hotel = null; public ?string $reservation = null; public ?string $specialEvent = null; public ?string $tickets = null; public function setBuildDay(string $day) : void { $this->day = $day; } public function setHotel(string $hotel) : void { $this->hotel = $hotel; } public function setReservation(string $reservation) : void { $this->reservation = $reservation; } public function setSpecialEvent(string $specialEvent) : void { $this->specialEvent = $specialEvent; } public function setTickents(string $tickets) : void { $this->tickets = $tickets; } }
index.php
use Builder\VacationBuilder; $builder = new VacationBuilder(); $builder->buildDay('2022/04/01'); $builder->addHotel('ホテル'); $builder->addTickents('パークチケット'); $builder->addReservation('夕食'); $yourPlanner = $builder->getVacationPlanner(); var_dump($yourPlanner); $builder = new VacationBuilder(); $builder->buildDay('2022/04/02'); $builder->addHotel('ホテル'); $builder->addSpecialEvent('パターンズオンアイス'); $builder->addReservation('夕食'); $yourPlanner = $builder->getVacationPlanner(); var_dump($yourPlanner); $builder = new VacationBuilder(); $builder->buildDay('2022/04/03'); $builder->addHotel('ホテル'); $builder->addSpecialEvent('パターンズサーカス'); $builder->addReservation('夕食'); $yourPlanner = $builder->getVacationPlanner(); var_dump($yourPlanner);
出力結果
object(Builder\Planner) (5) { ["day"]=> string(10) "2022/04/01" ["hotel"]=> string(9) "ホテル" ["reservation"]=> string(6) "夕食" ["specialEvent"]=> NULL ["tickets"]=> string(21) "パークチケット" } object(Builder\Planner) (5) { ["day"]=> string(10) "2022/04/02" ["hotel"]=> string(9) "ホテル" ["reservation"]=> string(6) "夕食" ["specialEvent"]=> string(30) "パターンズオンアイス" ["tickets"]=> NULL } object(Builder\Planner) (5) { ["day"]=> string(10) "2022/04/03" ["hotel"]=> string(9) "ホテル" ["reservation"]=> string(6) "夕食" ["specialEvent"]=> string(27) "パターンズサーカス" ["tickets"]=> NULL }