PHP Builder Model Learning notes

Source: Internet
Author: User
Tags abstract generator

Applicable scenario:

1, need to generate the Product object has a complex internal structure.
2. The properties of the product objects that need to be generated depend on each other, and the generator 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.

Use effect:

1, the use of generator mode allows the product's internal representation can be independently changed. Using the generator pattern 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.


<?php
/**
* Created by Phpstorm.
*/

/** Specific product role car
* Class Car
*/
Class car
{
Public $_head;
Public $_engine;//Engine
Public $_tyre;//Tires

Function Show ()
{
echo "The color of the headlights: {$this->_head}<br/>";
echo "engine brand: {$this->_engine}<br/>";
echo "Tyre brand: {$this->_tyre}<br/>";
}
}

/** Abstract car Builder (builder)
* Class Carbuilder
*/
Abstract class Carbuilder
{
protected $_car;

function __construct ()
{
$this->_car=new car ();
}

Abstract function Buildhead ();
Abstract function buildengine ();
Abstract function Buildtyre ();
Abstract function Getcar ();
}

/** Concrete Car Builder (generator) BMW
* Class BMW
*/
Class BMW extends Carbuilder
{

function Buildhead ()
{
Todo:implement Builderhead () method.
$this->_car->_head= "BLACK";
}

function Buildengine ()
{
Todo:implement Buildengine () method.
$this->_car->_engine= "BMW";
}

function Buildtyre ()
{
Todo:implement Buildtyre () method.
$this->_car->_tyre= "BMW";
}

function Getcar ()
{
Todo:implement Getcar () method.
return $this->_car;
}
}

/** Buick
* Class Buickbird
*/
Class Buickbird extends Carbuilder
{

function Buildhead ()
{
Todo:implement Buildhead () method.
$this->_car->_head= "Red";
}

function Buildengine ()
{
Todo:implement buildengine () Bmmethod.
$this->_car->_engine= "Buick";
}

function Buildtyre ()
{
Todo:implement Buildtyre () method.
$this->_car->_tyre= "Buick";
}

function Getcar ()
{
Todo:implement Getcar () method.
return $this->_car;
}
}

/** Conductor
* Class Director
*/
Class Director
{
/**
* @param $_builder Builders
* @return Mixed product Category: Car
*/
function construct ($_builder)
{
$_builder->buildhead ();
$_builder->buildengine ();
$_builder->buildtyre ();
return $_builder->getcar ();
}
}

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.