Applicable scenario:
1, need to generate the Product object has a complex internal structure.
2. The properties of the product objects that need to be generated depend on each other, and the generator pattern can be forced to generate order.
3. Some of the other objects in the system are used during object creation, which are not readily available during the creation of the Product object.
Use effect:
1, the use of generator mode allows the product's internal representation can be independently changed. Using the generator pattern allows the client to not know the details of the internal composition of the product.
2, each builder is relatively independent, and with other builder independent.
3. The final product constructed by the model is more easily controlled.
<?php
/**
* Created by Phpstorm.
*/
/** Specific product role car
* Class Car
*/
Class car
{
Public $_head;
Public $_engine;//Engine
Public $_tyre;//Tires
Function Show ()
{
echo "The color of the headlights: {$this->_head}<br/>";
echo "engine brand: {$this->_engine}<br/>";
echo "Tyre brand: {$this->_tyre}<br/>";
}
}
/** Abstract car Builder (builder)
* Class Carbuilder
*/
Abstract class Carbuilder
{
protected $_car;
function __construct ()
{
$this->_car=new car ();
}
Abstract function Buildhead ();
Abstract function buildengine ();
Abstract function Buildtyre ();
Abstract function Getcar ();
}
/** Concrete Car Builder (generator) BMW
* Class BMW
*/
Class BMW extends Carbuilder
{
function Buildhead ()
{
Todo:implement Builderhead () method.
$this->_car->_head= "BLACK";
}
function Buildengine ()
{
Todo:implement Buildengine () method.
$this->_car->_engine= "BMW";
}
function Buildtyre ()
{
Todo:implement Buildtyre () method.
$this->_car->_tyre= "BMW";
}
function Getcar ()
{
Todo:implement Getcar () method.
return $this->_car;
}
}
/** Buick
* Class Buickbird
*/
Class Buickbird extends Carbuilder
{
function Buildhead ()
{
Todo:implement Buildhead () method.
$this->_car->_head= "Red";
}
function Buildengine ()
{
Todo:implement buildengine () Bmmethod.
$this->_car->_engine= "Buick";
}
function Buildtyre ()
{
Todo:implement Buildtyre () method.
$this->_car->_tyre= "Buick";
}
function Getcar ()
{
Todo:implement Getcar () method.
return $this->_car;
}
}
/** Conductor
* Class Director
*/
Class Director
{
/**
* @param $_builder Builders
* @return Mixed product Category: Car
*/
function construct ($_builder)
{
$_builder->buildhead ();
$_builder->buildengine ();
$_builder->buildtyre ();
return $_builder->getcar ();
}
}