Builder 模式强调的是一步步创建对象,并通过相同的创建过程可以获
得不同的结果对象,一般来说 Builder 模式中对象不是直接返回的。
<?php标签:function,builder,描述语言,建造,method,Implement,PHP,TODO,public From: https://www.cnblogs.com/wmzll/p/11091259.html
/**
* 建造者模式
*
* 将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
*/
class BuilderModern
{
public function buildHead(){}
public function buildBody(){}
public function buildLeftArm(){}
public function buildRightArm(){}
public function buildLeftLeg(){}
public function buildRightLeg(){}
}
// 构建一个高个子的
class HeigthPeople extends BuilderModern
{
public function buildBody()
{
// TODO: Implement buildBody() method.
}
public function buildLeftArm()
{
// TODO: Implement buildLeftArm() method.
}
public function buildHead()
{
// TODO: Implement buildHead() method.
}
public function buildRightArm()
{
// TODO: Implement buildRightArm() method.
}
public function buildLeftLeg()
{
// TODO: Implement buildLeftLeg() method.
}
public function buildRightLeg()
{
// TODO: Implement buildRightLeg() method.
}
}
// 构建一个矮个子的
class ShortPeople extends BuilderModern
{
public function buildBody()
{
// TODO: Implement buildBody() method.
}
public function buildLeftArm()
{
// TODO: Implement buildLeftArm() method.
}
public function buildHead()
{
// TODO: Implement buildHead() method.
}
public function buildRightArm()
{
// TODO: Implement buildRightArm() method.
}
public function buildLeftLeg()
{
// TODO: Implement buildLeftLeg() method.
}
public function buildRightLeg()
{
// TODO: Implement buildRightLeg() method.
}
}
class Manage
{
public $builder = null;
public function __construct(Builder $builder)
{
$this->builder = new $builder();
}
public function create()
{
$this->builder->buildHead();
$this->builder->buildBody();
$this->builder->buildLeftArm();
$this->builder->buildRightArm();
$this->builder->buildLeftLeg();
$this->builder->buildRightLeg();
}
}