<?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();
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
-
-
-
七种PHP开发环境搭建工具
对于php开发小白来说搭建一个php运行环境就是一道坎!因为要做php开发,搭建一个能够运行php网站的服务器环境是第一步,传统的php环境软件非常复杂,好在很...
[阅读全文]
-
PHP实现抽奖功能实例代码
在项目开发中经常会遇到花钱抽奖类型的需求。但是老板总是担心用户用小钱抽到大奖。这样会导致项目亏损。下边这段代码可以有效制止抽奖项目亏钱。个人奖池:语言:thin...
[阅读全文]
-
-
-
PHP 超级全局变量相关总结
超级全局变量在php 4.1.0之后被启用, 是php系统中自带的变量,在一个脚本的全部作用域中都可用。php 超级全局变量php中预定义了几个超级全局变量(s...
[阅读全文]
网友评论