国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

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

2024-05-04 23:17:53
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:


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

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


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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 靖州| 汶上县| 麟游县| 齐齐哈尔市| 红原县| 襄垣县| 青铜峡市| 乌拉特中旗| 屯留县| 铅山县| 疏勒县| 日喀则市| 延吉市| 汉中市| 淮北市| 平度市| 壶关县| 汝城县| 荥阳市| 大连市| 宁陕县| 健康| 辉县市| 平江县| 濉溪县| 肇东市| 句容市| 宣恩县| 永仁县| 贡嘎县| 东乡族自治县| 大田县| 灯塔市| 札达县| 焦作市| 且末县| 宽城| 大庆市| 高要市| 灵山县| 定边县|