php設計模式 Builder(建造者模式)
更新時間:2011年06月26日 11:24:10 作者:
將一個復雜對象的構建與它的表示分離,使用同樣的構建過程可以創(chuàng)建不同的表示
復制代碼 代碼如下:
<?php
/**
* 建造者模式
*
* 將一個復雜對象的構建與它的表示分離,使用同樣的構建過程可以創(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();
相關文章
使用php+apc實現上傳進度條且在IE7下不顯示的問題解決方法
本篇文章介紹了,使用php+apc實現上傳進度條且在IE7下不顯示的問題解決方法。需要的朋友參考下2013-04-04PHP的curl實現get,post和cookie(實例介紹)
本篇文章是對PHP的curl實現get,post和cookie的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP PDOStatement::setFetchMode講解
今天小編就為大家分享一篇關于PHP PDOStatement::setFetchMode講解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02