The builder mode of PHP design mode

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

  • 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.