Php design mode Builder mode _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.