发布于 2014-12-04 05:54:11 | 264 次阅读 | 评论: 0 | 来源: 网友投递

本文为大家提供的是php设计模式中的 Builder(建造者模式)示例代码,感兴趣的同学参考下。

将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示


<?php
/**
* 建造者模式
*
* 将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示
*/
class Product
{
public $_type = null;
public $_size = null;
public $_color = null;

public function setType($type)
{
echo "set product type<br/>";
$this->_type = $type;
}

public function setSize($size)
{
echo "set product size<br/>";
$this->_size = $size;
}

public function setColor($color)
{
echo "set product color<br/>";
$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---<br/>";
$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();
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务