欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php設(shè)計(jì)模式 Builder(建造者模式)

 更新時(shí)間:2011年06月26日 11:24:10   作者:  
將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離,使用同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示
復(fù)制代碼 代碼如下:

<?php
/**
* 建造者模式
*
* 將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離,使用同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示
*/
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",
);

// 沒(méi)有使用bulider以前的處理
$oProduct = new Product();
$oProduct->setType($config['type']);
$oProduct->setSize($config['size']);
$oProduct->setColor($config['color']);


// 創(chuàng)建一個(gè)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();

相關(guān)文章

最新評(píng)論