作者:bakurei_492 | 来源:互联网 | 2023-09-03 14:24
1模式介绍是将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示2.类设计 <?php***建造者模式*copyright(c
1 模式介绍
是将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
2.类设计
php
/**
* 建造者模式
* copyright (c) http://blog.csdn.net/CleverCode
*/
//需要建造的产品(product)
class Car
{/*{{{*/
public $name;
public $engine;//发动机
public $chassis;//底盘
public $body;//车身
public $equipment;//电器设备
public function setName($name)
{
$this->name = $name;
}
public function setEngine($engine)
{
$this->engine = $engine;
}
public function setChassis($chassis)
{
$this->chassis = $chassis;
}
public function setBody($body)
{
$this->body = $body;
}
public function setEquipment($equipment)
{
$this->equipment = $equipment;
}
public function show()
{
echo "名称:".$this->name."\r\n";
echo "引擎:".$this->engine."\r\n";
echo "底盘:".$this->chassis."\r\n";
echo "车身:".$this->body."\r\n";
echo "电子设备:".$this->equipment."\r\n";
}
}/*}}}*/
//builder
interface IBuilder
{/*{{{*/
public function builderName();
public function builderEngine();
public function builderChassis();
public function builderBody();
public function builderEquipment();
public function getCar();
}/*}}}*/
//红旗车builder
class RedBuilder implements IBuilder
{/*{{{*/
public $car;
public function __construct()
{
$this->car = new Car();
}
public function builderName()
{
$this->car->setName('红旗');
}
public function builderEngine()
{
$this->car->setEngine('国产发动机');
}
public function builderChassis()
{
$this->car->setChassis('超大底盘');
}
public function builderBody()
{
$this->car->setBody('超大');
}
public function builderEquipment()
{
$this->car->setEquipment('电子设备');
}
public function getCar()
{
return $this->car;
}
}/*}}}*/
//QQ车builder
class QQBuilder implements IBuilder
{/*{{{*/
public $car;
public function __construct()
{
$this->car = new Car();
}
public function builderName()
{
$this->car->setName('QQ');
}
public function builderEngine()
{
$this->car->setEngine('国产发动机');
}
public function builderChassis()
{
$this->car->setChassis('小底盘');
}
public function builderBody()
{
$this->car->setBody('小');
}
public function builderEquipment()
{
$this->car->setEquipment('电子设备');
}
public function getCar()
{
return $this->car;
}
}/*}}}*/
//组装者(director)
class CarDirector
{/*{{{*/
public function make(IBuilder $builder)
{
$builder->builderName();
$builder->builderEngine();
$builder->builderChassis();
$builder->builderBody();
$builder->builderEquipment();
return $builder->getCar();
}
}/*}}}*/
class Client
{/*{{{*/
public static function main($argv)
{
$director = new CarDirector();
$redBuilder = new RedBuilder();
$car = $director->make($redBuilder);
$car->show();
echo "\r\n";
$qqBuilder = new QQBuilder();
$car = $director->make($qqBuilder);
$car->show();
}
}/*}}}*/
Client::main($argv);
?>