频道栏目
首页 > 资讯 > 代码收藏 > 正文

php设计模式 Builder(建造者模式)

16-01-07        来源:[db:作者]  
收藏   我要投稿
复制代码 代码如下:
/**
* 建造者模式
*
* 将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示
*/
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();
相关TAG标签
上一篇:zend studio 5.5 为什么不能复制源码!解决思路
下一篇:尽对进门知识: PHP与MYSQL把持实例讲解
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站