Copy Code code as follows:
<?php
/**
* Builders ' model
*
* Separate the build of a complex object from its representation, and use the same build process to create different representations
*/
Class Product
{
public $_type = null;
public $_size = null;
public $_color = null;
Public Function SetType ($type)
{
echo "Set product type<br/>";
$this->_type = $type;
}
Public Function setSize ($size)
{
echo "Set product size<br/>";
$this->_size = $size;
}
Public Function SetColor ($color)
{
echo "Set product color<br/>";
$this->_color = $color;
}
}
$config = Array (
"Type" => "shirt",
"Size" => "XL",
"Color" => "red",
);
No previous processing using Bulider
$oProduct = new Product ();
$oProduct->settype ($config [' type ']);
$oProduct->setsize ($config [' size ']);
$oProduct->setcolor ($config [' Color ']);
Create a builder class
Class Productbuilder
{
var $_config = null;
var $_object = null;
Public Function Productbuilder ($config)
{
$this->_object = new Product ();
$this->_config = $config;
}
Public Function Build ()
{
echo "---in builder---<br/>";
$this->_object->settype ($this->_config[' type '));
$this->_object->setsize ($this->_config[' size '));
$this->_object->setcolor ($this->_config[' color ');
}
Public Function getproduct ()
{
return $this->_object;
}
}
$objBuilder = new Productbuilder ($config);
$objBuilder->build ();
$objProduct = $objBuilder->getproduct ();