PHP design pattern Three-----builder mode

Source: Internet
Author: User

1. Introduction

Intent: separate a complex construct from its representation so that the same build process can create different representations.

Main Solution: The main solution in the software system, sometimes facing the creation of "a complex object", which is usually made up of the sub-objects of the various parts with a certain algorithm, because of the change in demand, the various parts of this complex object often face drastic changes, But the algorithms that combine them are relatively stable.

when to use: some basic components do not change, and their combinations often change.

Advantages: 1, the builder is independent, easy to expand. 2, easy to control the details of risk.

Disadvantage: 1, the product must have common ground, the scope is limited. 2, if the internal changes complex, there will be a lot of construction class.

2. Structure Composition:

2.1 Creating components, product roles

2.2 Abstract Builders

2.3 Concrete Builders

2.4 Conductors

The specific code is as follows:

//Builder Mode//1. Product Rolesclassbird{ Public $_head;  Public $_wing;  Public $_foot; functionShow () {Echo' Head: '.$this->_head. ' <br/> '; Echo' Wing: '.$this->_wing. ' <br/> '; Echo' Foot: '.$this->_foot. ' <br/> '; }}//2. Abstract BuilderAbstract classbirdbuilder{protected $_bird; function__construct () {$this->_bird =NewBird (); }    Abstract functionBuildhead (); Abstract functionbuildwing (); Abstract functionBuildfoot (); Abstract functionGetbird ();}//3. Concrete BuildersclassBluebirdextendsbirdbuilder{functionBuildhead () {$this->_bird->_head = ' Blue '; }    functionbuildwing () {$this->_bird->_wing = ' Blue '; }    functionBuildfoot () {$this->_bird->_foot = ' Blue '; }    functionGetbird () {return $this-_bird; }}classRosebirdextendsbirdbuilder{functionBuildhead () {$this->_bird->_head = ' Red '; }    functionbuildwing () {$this->_bird->_wing = ' Black '; }    functionBuildfoot () {$this->_bird->_foot = ' Green '; }    functionGetbird () {return $this-_bird; }}//4 Conductorsclassdirector{ Public $bird; function__construct ($_builder ){        $_builder-Buildhead (); $_builder-buildwing (); $_builder-Buildfoot (); $this->bird =$_builder-Getbird (); }    functionGetbird () {return $this-Bird; }}classfun{ Public $head= ' '; function__construct () {$this->head = ' head '; Echo1; }}Header(' Content-type:text/html;charset=utf8 ');$blueBird=NewBluebird ();$director=NewDirector ($blueBird ); $director->getbird ()->show ();

Reference: http://blog.csdn.net/jhq0113/article/details/45268743

Http://www.runoob.com/design-pattern/builder-pattern.html

PHP design pattern Three-----builder mode

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.