Learn PHP design patterns PHP implements builder pattern_php skills

WBOY
Release: 2016-05-16 20:03:49
Original
881 people have browsed it

Builder mode can separate the internal representation of a product from the production process of the product, so that products with different internal representations can be generated.
1. Builder mode structure diagram

2. Main characters in Builder mode
Abstract builder (Builder) role: Define an abstract interface to standardize the construction of each component of the product (that is, standardize the method implementation of the specific builder). The specified methods must include construction methods and result return methods
Concrete Builder (ConcreteBuilder) role: Implement the methods defined by the abstract builder role. The specific builder is closely related to the business logic. The application will eventually create the product according to the business logic by calling the construction method implemented in this role. After the construction is completed, the built product instance will be returned through the result return method. Typically created externally by a client or an abstract factory.
Director role: The role of this role is to call the specific builder role to build products. The director has no direct relationship with the product category. It is a concrete abstract role that talks to the product category.
Product role: The complex object created by the builder under the guidance of the instructor
The director role deals directly with the client. It understands the client's business logic, splits the client's request to create a product into requests for product components, and then calls specific product roles to perform construction operations. It separates the client from the concrete builder.
3. Advantages and Disadvantages of Builder Mode
Advantages of the Builder pattern: The Builder pattern can well separate the implementation of an object from the related "business" logic, making it very easy to add (or change) the implementation without changing the event logic. .
Disadvantages of the Builder pattern: Modifications to the builder interface will result in modifications to all execution classes.
4. Usage scenarios and effects of Builder mode
Builder mode should be used in the following situations:
1. The product object that needs to be generated has a complex internal structure.
2. The properties of the product objects that need to be generated depend on each other, and the builder pattern can force the generation order.
3. During the object creation process, some other objects in the system will be used, and these objects are not easy to obtain during the creation process of product objects.
Using builder mode mainly has the following effects:
1. The use of builder mode allows the internal appearance of the product to be changed independently. Using the builder pattern eliminates the need for the client to know the details of the product's internal makeup.
2. Each Builder is relatively independent and has nothing to do with other Builders.
3. The final product built by the model is easier to control.
5. Builder mode and other modes
Abstract factory mode: In the abstract factory mode, a complete product object will be returned every time the factory object is called, and the client may return these products Assembled into a larger and more complex product, or maybe not. The builder pattern is different. It builds a complex product piece by piece, and the assembly process of this product occurs inside the builder. The difference between the two is whether there is an assembly process and where the assembly process occurs. These two design patterns can be used together. By calling a construction role, the client indirectly calls another factory role in the abstract factory pattern. Factory mode returns parts from different product families, while Builder mode assembles them.

Strategy mode: Builder mode is very close to strategy mode in structure. In fact, builder mode is a special case of strategy mode. The difference between the two lies in their different intentions. The builder pattern works on the client to build new objects bit by bit, while the purpose of the strategy pattern is to provide an abstract interface for the algorithm.

Builder pattern and template method pattern: After the builder pattern degenerates and loses the role of director, it can develop into the template method pattern (that is, placing the algorithm implementation of the construction process in the construction role).

Builder pattern and composition pattern: Composition pattern describes the structure of an object tree, while builder pattern can be used to describe the generation process of the object tree.
The above 4 points are from "Java and Patterns"
6. Builder mode PHP example

<&#63;php
/**
 * 产品
 * 此处仅以一个产品类中的字符串演示产品
 */
class Product {                          
 /**
 * 产品的组成部分集合
 */
 private $_parts;
 
 public function __construct() {
 $this->_parts = array();
 }
 
 public function add($part) {
 return array_push($this->_parts, $part);
 }
 
 public function show() {
 echo "the product include:";
 array_map('printf', $this->_parts);
 }
}
 
/**
 * 抽象建造者 
 */
abstract class Builder {
 
 /**
 * 产品零件构造方法1
 */
 public abstract function buildPart1();
 
 
 /**
 * 产品零件构造方法2
 */
 public abstract function buildPart2();
 
 
 /**
 * 产品返还方法
 */
 public abstract function getResult();
}
 
/**
 * 具体建造者
 */
class ConcreteBuilder extends Builder {
 
 private $_product;
 
 public function __construct() {
 $this->_product = new Product();
 }
 
 public function buildPart1() {
 $this->_product->add("Part1");
 }
 
 public function buildPart2() {
 $this->_product->add("Part2");
 }
 
 public function getResult() {
 return $this->_product;
 }
}
 
/**
 * 导演者
 */
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();
&#63;>
Copy after login

The above is the code for using PHP to implement the builder mode. There are also some conceptual distinctions about the builder mode. I hope it will be helpful to everyone's learning.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template