您好,欢迎访问一九零五行业门户网

PHP设计模式——建造者模式builder

建造者模式是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示,它着重于一步步构造一个复杂对象。
class product{ protected $_type = ''; protected $_size = ''; protected $_color = ''; public function settype($type){ $this->_type = $type; } public function setsize($size){ $this->_size = $size; } public function setcolor($color){ $this->_color = $color; } public function gettype(){ return $this->_type; } public function getsize(){ return $this->_size; } public function getcolor(){ return $this->_color; }}class productbuilder{ protected $_product = null; protected $_configs = array(); public function __construct($configs){ $this->_product = new product(); $this->_configs = $configs; } public function build(){ $this->_product->settype($this->_configs['type']); $this->_product->setsize($this->_configs['size']); $this->_product->setcolor($this->_configs['color']); } public function getproduct(){ return $this->_product; }}$configs = array('type'=>'shirt', 'size'=>'xl', 'color'=>'red');$builder = new productbuilder($configs);$builder->build();$product = $builder->getproduct();echo $product->getcolor();
以上就介绍了php设计模式——建造者模式builder,包括了对象方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息