Builder mode is created mode
Overview: Separating the construction of a complex object from its representation so that the same build process can create different representations
Advantages:
The builder pattern separates the implementation of an object from the associated ' business ' logic, making it easy to add (or change) implementations without changing the logic of the event.
Disadvantages:
Modification of the builder interface will result in modification of all execution classes
Builders should be used in the following situations:
1 product objects that need to be generated have complex internal structures
2 The properties of the product objects that need to be generated depend on each other, and the builder pattern can force the build order
3 Some other objects in the system are used during object creation, which are not easily available during the creation of the product
Using the builder model has the following effects:
The use of the 1 builder mode allows the internal representation of the product to change independently, using the builder mode so that the client does not have to know the details of the internal composition of the product
2 Each builder is relatively independent and not related to the other builder
The final product built in 3 mode is easier to control
Class product{
public $type = null;
public $price = null;
public $color = null;
Public Function SetType ($type) {
$this->type = $type;
}
Public Function Setprice ($price) {
$this->price = $price;
}
Public Function SetColor ($color) {
$this->color = $color;
}
}
$config = Array (
' Type ' = ' shirt ',
' Price ' = 100,
' Color ' = ' red ',
);
Do not use builder mode
$product = new product ();
$product->settype ($config [' type ']);
$product->setprice ($config [' Price ']);
$product->setcolor ($config [' Color ']);
Using the builder mode
/*builder class */
Class productbuilder{
public $config = null;
public $object = null;
Public function __construct ($config) {
$this->object = new Product ();
$this->config = $config;
}
Public Function build () {
$this->object->settype ($this->config[' type ');
$this->object->setprice ($this->config[' price ');
$this->object->setcolor ($this->config[' color ');
}
Public fuction getproduct () {
return $this->object;
}
}
$objBuilder = new Productbuilder ($config);
$objBuilder->build ();
$objProduct = $objBuilder->getproduct ();
Var_dump ($objProduct);