php设计模式 Builder(建造者模式)
2015-01-24信息快讯网
将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示
<?php
/**
* 建造者模式
*
* 将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示
*/
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']);
// 创建一个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();
Yii查询生成器(Query Builder)用法实例教程
yii框架builder、update、delete使用方法
php读取EXCEL文件 php excelreader读取excel文件
php excel reader读取excel内容存入数据库实现代码
header跳转和include包含问题详解
PHP中使用foreach和引用导致程序BUG的问题介绍
php 调试利器debug_print_backtrace()
header中Content-Disposition的作用与使用方法
php中http_build_query 的一个问题
PHP header函数分析详解
在WAMP环境下搭建ZendDebugger php调试工具的方法
PHP 数据结构 算法描述 冒泡排序 bubble sort
php设计模式 Adapter(适配器模式)
php设计模式 DAO(数据访问对象模式)
php性能优化分析工具XDebug 大型网站调试工具
PHP错误抑制符(@)导致引用传参失败Bug的分析
PHP 调试工具Debug Tools
php debug 安装技巧
fleaphp rolesNameField bug解决方法
The specified CGI application misbehaved by not returning a complete set of HTTP headers
php header示例代码(推荐)
PHP下利用header()函数设置浏览器缓存的代码
PHP通过header实现文本文件下载的代码
PHP 页面编码声明方法详解(header或meta)