Learn PHP Design patterns PHP Implementation Builder mode _php skills

Source: Internet
Author: User
Tags learn php

The builder model allows the internal representation of a product to be separated from the production process of the product, thus producing products with different internal representations.
First, builder pattern structure diagram

second, the main role in the builder model
Abstract Builder (Builder) role:Define an abstract interface that regulates the construction of each component of the product (i.e. the method of standardizing the concrete builder). The method must include construction method and result return method.
specific Builder (concretebuilder) role:Implements the method defined by the abstract builder role. The concrete builder is associated with the business logic, and the application eventually creates the product according to the business logic by invoking the build method implemented in this role, returning the constructed product instance through the result return method after the construction completes. Typically created externally by a customer or an abstract factory.
Director (Director) Role:The role of this role is to invoke specific builder roles to build products. The Director is not directly related to the product class, and the product class is a concrete abstract role to talk to.
Product Role:The complex object created by the builder under the guidance of the Mentor
The director role deals directly with the client, understands the client's business logic, splits the client's request to create a product into a request for a product component, and then invokes a specific product role to perform the build operation. It separates the client from the concrete builder.
Iii. Advantages and disadvantages of the builder model
The advantage of the builder model is that the builder model can well separate the implementation of an object from the related "business" logic, making it easy to add (or change) implementation without altering the logic of the event.
Disadvantages of the builder model: modification of the builder interface can cause changes to all execution classes.
Four, the use scene and effect of builder mode
The builder pattern should be used in the following situations:
1, need to generate the Product object has a complex internal structure.
2. The attributes of the product objects that need to be generated depend on each other, and the builder pattern can be forced to generate order.
3. Some of the other objects in the system are used during object creation, which are not readily available during the creation of the Product object.
The use of builder mode mainly has the following effects:
1, the use of the builder model makes the internal appearance of the product can be independently changed. The use of Builder mode allows the client to not know the details of the internal composition of the product.
2, each builder is relatively independent, and with other builder independent.
3. The final product constructed by the model is more easily controlled.
v. Builder mode and other modes
Abstract Factory mode (in abstract Factory mode):In abstract Factory mode, every time a factory object is invoked, a complete product object is returned, and the client may assemble the product into a larger, more complex product, or it may not. The builder model is different, and it builds a complex product at 1.1 points, and the assembly process of the product occurs within the builder. The difference is whether there is an assembly process, where the assembly process takes place. These two design patterns can be linked together, and the client invokes another factory role in the abstract factory pattern indirectly by invoking a build role. Factory models return parts of different product families, and the builder model assembles them.

strategy Mode (Strategy mode): The builder model is structurally close to the policy model, and in fact the builder pattern is a special case of the strategy pattern. The difference between the two is that the intention is different. The builder pattern acts on the client's 1.1-point construction of a new object, and the purpose of the policy pattern is to provide an abstract interface for the algorithm.

Builder model and template method pattern: The builder model can evolve into template method patterns (the implementation of algorithmic implementations of construction processes in the construction role) after degradation and loss of director roles.

The builder pattern and the synthesis pattern: The synthetic pattern describes the composition of an object tree, while the builder pattern can be used to describe the generation process of the object tree.
The above 4 points come from Java and schema
vi. Builder mode PHP sample

<?php/** * Products * Here only the strings in one product class Demo product/class Product {/** * product components set/private $_p
 
 Arts
 Public Function __construct () {$this->_parts = array ();
 Public function Add ($part) {return Array_push ($this->_parts, $part);
 The Public Function show () {echo "The product include:";
 Array_map (' printf ', $this->_parts);
 
 
 }/** * Abstraction Builder/abstract class Builder {/** * Product part Construction Method 1 */public abstract function buildPart1 ();
 
 
 /** * Product Part Construction Method 2 */public abstract function buildPart2 ();
/** * Product Return method * * Public abstract function getresult ();
 
 /** * Specific builder/class ConcreteBuilder extends Builder {private $_product;
 Public Function __construct () {$this->_product = new product ();
 The Public Function BuildPart1 () {$this->_product->add ("Part1");
 The Public Function BuildPart2 () {$this->_product->add ("Part2");
 The Public Function GetResult () {return $this->_product; }/** * DirectorPerson */class Director {public function __construct (Builder $builder) {$builder->buildpart1 ();
 $builder->buildpart2 ();
 } class Client {/** * Main program.
 */public static function main () {$buidler = new ConcreteBuilder ();
 $director = new Director ($buidler);
 $product = $buidler->getresult ();
 $product->show (); } client::main ();?>

This is the code that uses PHP to implement the builder pattern, and there are some conceptual distinctions about the builder's model that I hope will help you learn.

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.