/**
* 產品類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;
}
}
/*
抽象建造者:
定義的一個抽象接口,用于對具體建造者類進行規(guī)范
*/
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;
}
}
/*
導演者類:
用于調用具體建造者類創(chuàng)建產品類實例
*/
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;