Creational/Builder
This commit is contained in:
43
code/builder.php
Normal file
43
code/builder.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-04
|
||||
* @time: 00:19
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Creational\Builder\BigMacBuilder;
|
||||
use Pattern\Creational\Builder\Director;
|
||||
|
||||
/**
|
||||
* The client code creates a builder object, passes it to the director and then
|
||||
* initiates the construction process. The end result is retrieved from the
|
||||
* builder object.
|
||||
*
|
||||
* @noinspection ForgottenDebugOutputInspection*/
|
||||
function clientCode(Director $director): void
|
||||
{
|
||||
$builder = new BigMacBuilder();
|
||||
$director->setBuilder($builder);
|
||||
|
||||
echo 'Preparing full featured Big Mac...';
|
||||
$director->regularBigMac();
|
||||
dump($builder->getBigMac()->getIngredients());
|
||||
|
||||
echo 'Preparing vegan Big Mac...';
|
||||
$director->veganBigMac();
|
||||
dump($builder->getBigMac()->getIngredients());
|
||||
|
||||
// The Builder pattern can be used without a Director class.
|
||||
echo 'Preparing a custom snack...';
|
||||
$builder->produceBun();
|
||||
$builder->produceMeat();
|
||||
$builder->produceSauce();
|
||||
dump($builder->getBigMac()->getIngredients());
|
||||
}
|
||||
|
||||
|
||||
clientCode(new Director());
|
||||
Reference in New Issue
Block a user