[php]php设计模式 Builder(建造者模式)
2011-01-04 23:14
756 查看
<?php
/**
* 建造者模式
*
* 将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示
*/
class Product
{
public$_type=null;
public$_size=null;
public$_color=null;
publicfunctionsetType($type)
{
echo"set product type<br/>";
$this->_type =$type;
}
publicfunction setSize($size)
{
echo"set product size<br/>";
$this->_size =$size;
}
publicfunction 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;
publicfunction ProductBuilder($config)
{
$this->_object =new Product();
$this->_config =$config;
}
publicfunction build()
{
echo"--- in builder---<br/>";
$this->_object->setType($this->_config['type']);
$this->_object->setSize($this->_config['size']);
$this->_object->setColor($this->_config['color']);
}
publicfunction getProduct()
{
return$this->_object;
}
}
$objBuilder=new ProductBuilder($config);
$objBuilder->build();
$objProduct=$objBuilder->getProduct();
/**
* 建造者模式
*
* 将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示
*/
class Product
{
public$_type=null;
public$_size=null;
public$_color=null;
publicfunctionsetType($type)
{
echo"set product type<br/>";
$this->_type =$type;
}
publicfunction setSize($size)
{
echo"set product size<br/>";
$this->_size =$size;
}
publicfunction 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;
publicfunction ProductBuilder($config)
{
$this->_object =new Product();
$this->_config =$config;
}
publicfunction build()
{
echo"--- in builder---<br/>";
$this->_object->setType($this->_config['type']);
$this->_object->setSize($this->_config['size']);
$this->_object->setColor($this->_config['color']);
}
publicfunction getProduct()
{
return$this->_object;
}
}
$objBuilder=new ProductBuilder($config);
$objBuilder->build();
$objProduct=$objBuilder->getProduct();
相关文章推荐
- php设计模式 Builder(建造者模式)
- PHP设计模式——建造者模式(builder)
- PHP设计模式系列 - 建造者模式
- 乐在其中设计模式(C#) - 建造者模式(Builder Pattern)
- 设计模式之builder(建造者模式)
- 【设计模式学习笔记六】【创建型模式】【建造者模式(Builder)】
- 建造者模式(Builder Pattern)----设计模式
- Java设计模式----建造者模式(Builder)
- Java设计模式——创建型模式——建造者模式(Builder Pattern)
- 设计模式 之 《建造者模式(Builder)》
- C#设计模式之四建造者模式(Builder Pattern)【创建型】
- 【Unity3D与23种设计模式】建造者模式(Builder)
- 设计模式(2)-创建型-建造者模式(Builder)
- 23种设计模式-3.建造者模式(Builder)
- PHP设计模式 建造者模式
- 设计模式--建造者模式Builder(创建型)
- [工作中的设计模式]建造者模式builder
- 乐在其中设计模式(C#) - 建造者模式(Builder Pattern)
- Python实现设计模式--05.建造者模式(Builder Pattern)