- What classes does it consist of?
- What roles do these classes play?
- In what way the elements of the pattern are related?
interface Builder { public function producePartA(): void; public function producePartB(): void; public function producePartC(): void; }The RealBuilder classes follow the Builder interface and provides specific implementations of the building steps. The program may have several variations of Builders, implemented differently. We now build a class object called RealBuilder. The code for RealBuilder.php is as follows:
class RealBuilder implements Builder { private $product; public function __construct() { $this->reset(); } public function reset(): void { $this->product = new Product; } public function producePartA(): void { $this->product->parts[] = "PartA"; } public function producePartB(): void { $this->product->parts[] = "PartB"; } public function producePartC(): void { $this->product->parts[] = "PartC"; } public function getProduct(): Product { $result = $this->product; $this->reset(); return $result; } }Concrete Builders are supposed to provide their own methods for retrieving results. That's because various types of builders may create entirely different products that don't follow the same interface. Therefore, such methods cannot be declared in the base Builder interface (at least in a statically typed programming language). Note that PHP is a dynamically typed language and this method can be in the base interface. However, we won't declare it there for the sake of clarity.
class Product { public $parts = []; public function listParts(): void { echo "Product parts: " . implode(', ', $this->parts) . "Next we create a Director class. That file looks like:
"; } }
class Director { private $builder; public function setBuilder(Builder $builder): void { $this->builder = $builder; } public function buildMinimalViableProduct(): void { $this->builder->producePartA(); } public function buildFullFeaturedProduct(): void { $this->builder->producePartA(); $this->builder->producePartB(); $this->builder->producePartC(); } }Finally we go to the index.php an add some client code. First we need to add some include files:
include_once ('Builder.php'); include_once('RealBuilder.php');; include_once('Product.php'); include_once ('Director.php');for the client code we have:
function clientCode(Director $director) { $builder = new RealBuilder; $director->setBuilder($builder); echo "Standard basic product:Lastly we add some client code to test our code
"; $director->buildMinimalViableProduct(); $builder->getProduct()->listParts(); echo "Standard full featured product:
"; $director->buildFullFeaturedProduct(); $builder->getProduct()->listParts(); echo "Custom product:
"; $builder->producePartA(); $builder->producePartC(); $builder->getProduct()->listParts(); }
$director = new Director; clientCode($director); echo "endThe result we get is
";
Standard basic product: Product parts: PartA Standard full featured product: Product parts: PartA, PartB, PartC Custom product: Product parts: PartA, PartC endGet the code!!
Wikipedia
Find Ray on:
youtube
The Ray Code
Ray Andrade
No comments:
Post a Comment