php設(shè)計模式 Builder(建造者模式)
更新時間:2011年06月26日 11:24:10 作者:
將一個復(fù)雜對象的構(gòu)建與它的表示分離,使用同樣的構(gòu)建過程可以創(chuà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();
您可能感興趣的文章:
- PHP設(shè)計模式之建造者模式(Builder)原理與用法案例詳解
- PHP設(shè)計模式之建造者模式定義與用法簡單示例
- 學(xué)習(xí)php設(shè)計模式 php實(shí)現(xiàn)建造者模式
- PHP設(shè)計模式(一)工廠模式Factory實(shí)例詳解【創(chuàng)建型】
- PHP設(shè)計模式概論【概念、分類、原則等】
- PHP設(shè)計模式之 策略模式Strategy詳解【對象行為型】
- php設(shè)計模式 Template (模板模式)
- PHP常用的三種設(shè)計模式匯總
- php單態(tài)設(shè)計模式(單例模式)實(shí)例
- PHP經(jīng)典面試題之設(shè)計模式(經(jīng)常遇到)
- php設(shè)計模式小結(jié)
- PHP設(shè)計模式(三)建造者模式Builder實(shí)例詳解【創(chuàng)建型】
相關(guān)文章
PHP命名空間namespace定義及導(dǎo)入use用法詳解
這篇文章主要介紹了PHP命名空間namespace定義及導(dǎo)入use用法,結(jié)合實(shí)例形式詳細(xì)分析了php中命名空間namespace的功能、定義及導(dǎo)入use相關(guān)使用方法與操作技巧,需要的朋友可以參考下2018-03-03PHP __call()方法實(shí)現(xiàn)委托示例
這篇文章主要介紹了PHP __call()方法實(shí)現(xiàn)委托,簡單介紹了委托的概念、原理,并結(jié)合實(shí)例形式分析了__call()方法實(shí)現(xiàn)委托的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05php和html的區(qū)別點(diǎn)詳細(xì)總結(jié)
在本篇文章里小編給大家整理了關(guān)于php和html的區(qū)別點(diǎn),有需要的朋友們可以參考下。2019-09-09使用php+apc實(shí)現(xiàn)上傳進(jìn)度條且在IE7下不顯示的問題解決方法
本篇文章介紹了,使用php+apc實(shí)現(xiàn)上傳進(jìn)度條且在IE7下不顯示的問題解決方法。需要的朋友參考下2013-04-04PHP的curl實(shí)現(xiàn)get,post和cookie(實(shí)例介紹)
本篇文章是對PHP的curl實(shí)現(xiàn)get,post和cookie的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP PDOStatement::setFetchMode講解
今天小編就為大家分享一篇關(guān)于PHP PDOStatement::setFetchMode講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02