第十三章 好菜每回味不同 --- 建造者模式
<?php
//画小人
abstract class PersonBuilder
{
abstract public function BuildHead();
abstract public function BuildBody();
abstract public function BuildArmLeft();
abstract public function BuildArmRight();
abstract public function BuildLegLeft();
abstract public function BuildLegRight();
}
class PersonThinBuilder extends PersonBuilder
{
public function BuildHead()
{
echo "小头\n";
}
public function BuildBody()
{
echo "小身子\n";
}
public function BuildArmRight()
{
echo "右臂\n";
}
public function BuildArmLeft()
{
echo "左臂\n";
}
public function BuildLegLeft()
{
echo "左腿\n";
}
public function BuildLegRight()
{
echo "右腿\n";
}
}
class PersonFatBuilder extends PersonBuilder
{
public function BuildHead()
{
echo "大头\n";
}
public function BuildBody()
{
echo "大身子\n";
}
public function BuildArmRight()
{
echo "右臂\n";
}
public function BuildArmLeft()
{
echo "左臂\n";
}
public function BuildLegLeft()
{
echo "左腿\n";
}
public function BuildLegRight()
{
echo "右腿\n";
}
}
class PersonDirector
{
private $personBuilder;
function __construct($personBuilder)
{
$this->personBuilder = $personBuilder;
}
public function CreatePerson()
{
$this->personBuilder->BuildHead();
$this->personBuilder->BuildBody();
$this->personBuilder->BuildArmRight();
$this->personBuilder->BuildArmLeft();
$this->personBuilder->BuildLegLeft();
$this->personBuilder->BuildLegRight();
}
}
//客户端代码
echo "苗条的:\n";
$thinDirector = new PersonDirector(new PersonThinBuilder());
$thinDirector->CreatePerson();
echo "\n胖的:\n";
$fatDirector = new PersonDirector(new PersonFatBuilder());
$fatDirector->CreatePerson();
总结:
建造者模式,将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
如果我们用了建造者模式,那么用户只需要指定需要建造的类型就可以得到他们,而具体建造的过程和细节就不需要知道了。
主要用于创建一些复杂的对象,这些对象内部构建间的建造顺序通常是稳定的,但对象内部的构建通畅面临着复杂的变化。
建造者模式是在当创建复杂对象的算法应该独立于改对象的组成部分以及它们的装配方式时适用的模式。
当前内容版权归 flyingalex 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 flyingalex .