多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ <?php class Product {//产品本身 private $_parts; public function __construct(){ $this->_parts = array(); } public function add($parts){return array_push($this->_parts,$parts);} } abstract class Builder{//建造者抽象类 public abstract function buildPart1(); public abstract function buildPart2(); public abstract function getResult(); } class ConCreateBuilder extends Builder{//具体建造者 private $_product; public function __construct(){ $this->_product = new Product(); } public function buildPart1(){$this->_product->add('part1');} public function buildPart2(){$this->_product->add('part2');} public function getResult(){return $this->_product;} } class Dreator{//导演者 public function __construct(ConCreateBuilder $conCreateBuilder){ $conCreateBuilder->buildPart1(); $conCreateBuilder->buildPart2(); } } $builder = new ConcreateBuilder(); $dreator = new Dreator($builder); var_dump($builder->getResult()); ~~~ 传递具体建造者的实例到导演者的构造函数里 [root@centos6 dp]# php builder.php object(Product)#2 (1) { ["_parts":"Product":private]=> array(2) { [0]=> string(5) "part1" [1]=> string(5) "part2" } }