/**
* 产品类Person
*/
class Person
{
public $_head;
public $_body;
public function setHead($head){
$this->_head=$head;
}
public function getHead(){
echo $this->_head;
}
public function setBody($body){
$this->_body=$body;
}
public function getBody(){
echo $this->_body;
}
}
/*
抽象建造者:
定义的一个抽象接口,用于对具体建造者类进行规范
*/
interface Builder{
public function buildHead();
public function buildBody();
public function getResult();
}
/*
具体建造者:
用于实现具体建造者类
*/
class ConcreteBuilder implements Builder{
public $person;
public $data;
public function __construct($data){
$this->person=new Person();
$this->data=$data;
}
public function buildHead(){
$this->person->setHead($this->data['head']);
}
public function buildBody(){
$this->person->setBody($this->data['body']);
}
public function getResult(){
return $this->person;
}
}
/*
导演者类:
用于调用具体建造者类创建产品类实例
*/
class Director{
public function __construct(ConcreteBuilder $builder){
$builder->buildHead();
$builder->buildBody();
}
}
/*
客户端:
根据需求进行逻辑处理
*/
$data=array(
'head'=>'大头儿子',
'body'=>'身体棒棒哒'
);
$builder=new ConcreteBuilder($data);
$director=new Director($builder);
$person=$builder->getResult();
echo $person->_head;
echo $person->_body;