国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 广饶县| 冷水江市| 周至县| 东莞市| 阿勒泰市| 龙江县| 东平县| 乌鲁木齐县| 吉木萨尔县| 嫩江县| 天峨县| 诸城市| 怀宁县| 绥化市| 石河子市| 吴堡县| 吉安市| 浮梁县| 商南县| 临沭县| 涪陵区| 昔阳县| 南充市| 民勤县| 广宗县| 确山县| 江油市| 开江县| 秭归县| 松滋市| 永新县| 正镶白旗| 阜宁县| 阿拉善右旗| 高唐县| 保山市| 靖州| 阿克| 汉阴县| 宣恩县| 丰都县|