Home >Backend Development >PHP Tutorial >PHP Design Patterns - Builder Pattern_PHP Tutorial
The builder pattern is also called the generator pattern. The core idea is to separate the construction of a complex object from its representation, so that the same construction process can create different representations. This design pattern is called the builder pattern.
For example: a car, its engines come in many brands, its tires are made of various materials, and its interiors are all kinds of strange; a bird, its head, wings and feet come in various colors and shapes, when creating such complex objects Sometimes, we recommend using the builder pattern.
Class diagram:
Builder mode is generally considered to have four roles:
1. Product role, product role defines its own composition attributes
2. Abstract builder, the abstract builder defines the product creation process and how to return a product
3. Concrete builder, the concrete builder implements the method of the abstract builder to create the product process, and assigns values to the specific attributes of the product
4. Commander, the commander is responsible for interacting with the calling client and deciding what kind of product to create
Code:
<!--?php /** * Created by PhpStorm. * User: Jiang * Date: 2015/4/25 * Time: 9:31 */ /**具体产品角色 鸟类 * Class Bird */ class Bird { public $_head; public $_wing; public $_foot; function show() { echo 头的颜色:{$this--->_head} ; echo 翅膀的颜色:{$this->_wing} ; echo 脚的颜色:{$this->_foot} ; } } /**抽象鸟的建造者(生成器) * Class BirdBuilder */ abstract class BirdBuilder { protected $_bird; function __construct() { $this->_bird=new Bird(); } abstract function BuildHead(); abstract function BuildWing(); abstract function BuildFoot(); abstract function GetBird(); } /**具体鸟的建造者(生成器) 蓝鸟 * Class BlueBird */ class BlueBird extends BirdBuilder { function BuildHead() { // TODO: Implement BuilderHead() method. $this->_bird->_head=Blue; } function BuildWing() { // TODO: Implement BuilderWing() method. $this->_bird->_wing=Blue; } function BuildFoot() { // TODO: Implement BuilderFoot() method. $this->_bird->_foot=Blue; } function GetBird() { // TODO: Implement GetBird() method. return $this->_bird; } } /**玫瑰鸟 * Class RoseBird */ class RoseBird extends BirdBuilder { function BuildHead() { // TODO: Implement BuildHead() method. $this->_bird->_head=Red; } function BuildWing() { // TODO: Implement BuildWing() method. $this->_bird->_wing=Black; } function BuildFoot() { // TODO: Implement BuildFoot() method. $this->_bird->_foot=Green; } function GetBird() { // TODO: Implement GetBird() method. return $this->_bird; } } /**指挥者 * Class Director */ class Director { /** * @param $_builder 建造者 * @return mixed 产品类:鸟 */ function Construct($_builder) { $_builder->BuildHead(); $_builder->BuildWing(); $_builder->BuildFoot(); return $_builder->GetBird(); } }
header(Content-Type:text/html;charset=utf-8); //------------------------生成器模式测试代码------------------ require_once ./Builder/Builder.php; $director=new Director(); echo 蓝鸟的组成: