Home > Backend Development > PHP Tutorial > windowsbuilder php design mode Builder builder mode

windowsbuilder php design mode Builder builder mode

WBOY
Release: 2016-07-29 08:45:49
Original
958 people have browsed it

复制代码 代码如下:


/**
* Builder pattern
*
* Separate the construction of a complex object from its representation, and use the same construction process to create different representations
*/
class Product
{
public $_type = null;
public $_size = null;
public $_color = null;
public function setType($type)
{
echo "set product type
";
$this->_type = $type;
}
public function setSize($size)
{
echo "set product size
";
$this->_size = $size;
}
public function setColor($color)
{
echo "set product color
";
$this->_color = $color;
}
}
$config = array(
"type"=>"shirt",
"size"=>"xl",
"color"=>"red",
);
// 没有使用bulider以前的处理
$oProduct = new Product();
$oProduct->setType($config['type']);
$oProduct->setSize($config['size']);
$oProduct->setColor($config['color']);
// 创建一个builder类
class ProductBuilder
{
var $_config = null;
var $_object = null;
public function ProductBuilder($config)
{
$this->_object = new Product();
$this->_config = $config;
}
public function build()
{
echo "--- in builder---
";
$this->_object->setType($this->_config['type']);
$this->_object->setSize($this->_config['size']);
$this->_object->setColor($this->_config['color']);
}
public function getProduct()
{
return $this->_object;
}
}
$objBuilder = new ProductBuilder($config);
$objBuilder->build();
$objProduct = $objBuilder->getProduct();

以上就介绍了windowsbuilder php设计模式 Builder建造者模式,包括了windowsbuilder方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template