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