Php design pattern Builder (Builder pattern ). Copy the code as follows :? Php *** builder mode ** separates the construction of a complex object from its representation. different representations can be created using the same construction process * classProduc
The code is as follows:
/**
* Builder mode
*
* Separates the construction of a complex object from its representation. different representations can be created using the same construction process.
*/
Class Product
{
Public $ _ type = null;
Public $ _ size = null;
Public $ _ color = null;
Public function setType ($ type)
{
Echo "set product type
";
$ This-> _ type = $ type;
}
Public function setSize ($ size)
{
Echo "set product size
";
$ This-> _ size = $ size;
}
Public function setColor ($ color)
{
Echo "set product color
";
$ This-> _ color = $ color;
}
}
$ Config = array (
"Type" => "shirt ",
"Size" => "xl ",
"Color" => "red ",
);
// The previous bulider processing is not used
$ 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 ---
";
$ 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 ();
The http://www.bkjia.com/PHPjc/323781.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323781.htmlTechArticle code is as follows :? Php/*** builder mode ** separates the construction of a complex object from its representation. you can use the same construction process to create different representations */class Produc...