Home > Backend Development > PHP Tutorial > PHP Design Patterns - Builder Pattern_PHP Tutorial

PHP Design Patterns - Builder Pattern_PHP Tutorial

WBOY
Release: 2016-07-13 09:55:26
Original
926 people have browsed it

PHP Design Pattern - Builder Pattern

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();
    }
}
Copy after login

Call the client test code:

header(Content-Type:text/html;charset=utf-8);
//------------------------生成器模式测试代码------------------
require_once ./Builder/Builder.php;

$director=new Director();

echo 蓝鸟的组成:
Copy after login

; $blue_bird=$director->Construct(new BlueBird()); $blue_bird->Show(); echo
The composition of Rose bird:
; $rose_bird=$director->Construct(new RoseBird()); $rose_bird->Show();
PHP object-oriented design patterns

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/991915.htmlTechArticlePHP Design Pattern - Builder Pattern The builder pattern is also called the generator pattern. The core idea is to convert a complex object into The construction of is separated from its representation, allowing the same building process to create different...
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