PHP design mode-builder Mode _php tutorial

Source: Internet
Author: User

PHP design mode-builder mode


The builder pattern is also called the generator pattern, and the core idea is to separate the construction of a complex object from its representation so that the same build process can create different representations, which are called builder patterns.

For example: Cars, his engine engine has a lot of brands, tires also have a variety of materials, the interior is more strange; birds, his head, wings and feet have a variety of colors and shapes, when creating this complex object, we recommend using the Builder mode.

Class Diagram:

The builder pattern is generally considered to have four characters:

1. Product roles, product roles define the constituent attributes of themselves

2. Abstract builder, abstract builder defines the process of creating a product and how to return a product

3. Concrete Builders, concrete builders implement methods to create product processes for abstract builders, assign definitions to specific attributes of a product

4. The conductor, the conductor is responsible for interacting with the calling client and deciding what product to create

Code:

 _head};        Color of Echo Wings: {$this->_wing};    Color of echo feet: {$this->_foot};    }}/** Abstract Bird Builder (Generator) * 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 ();} /** Concrete Bird Builder (Generator) Bluebird * Class Bluebird */class Bluebird extends birdbuilder{function Buildhead () {//Todo:i        Mplement 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; }}/** Rose 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; }}/** conductor * Class Director */class director{/** * @param $_builder Builder * @return Mixed product Category: Bird *        /function Construct ($_builder) {$_builder->buildhead ();        $_builder->buildwing ();        $_builder->buildfoot ();    return $_builder->getbird (); }}

To invoke the client test code:

Header (content-type:text/html;charset=utf-8);//------------------------Generator mode test code------------------require_once ./builder/builder.php; $director =new Director (); The composition of the Echo Blue Bird:
; $blue _bird= $director->construct (New Bluebird ()); $blue _bird->show (); Echo
The composition of the Rose Bird: ; $rose _bird= $director->construct (New Rosebird ()); $rose _bird->show ();
PHP Object-oriented design pattern

http://www.bkjia.com/PHPjc/991915.html www.bkjia.com true http://www.bkjia.com/PHPjc/991915.html techarticle php Design Pattern-builder Mode builder mode also known as the generator pattern, the core idea is to separate the construction of a complex object from its representation, so that the same build process can be created not ...

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