php设计模式之建造者模式

Posted running-fly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php设计模式之建造者模式相关的知识,希望对你有一定的参考价值。

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);
?>

 

以上是关于php设计模式之建造者模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式之:建造者模式BuilderPattern的实现

23种设计模式之建造者模式代码实例

设计模式从青铜到王者第八篇:创建型模式之建造者模式(BuilderPattern)

《设计模式》之建造者模式(Builder)

创建型模式之建造者模式

设计模式之建造者模式