@Chiang
2019-12-12T13:37:20.000000Z
字数 3833
阅读 574
设计模式
- 抽象建造者 (interface) 接口
- 具体建造者
- 导演者
- 产品角色
造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。建造者模式属于对象创建型模式。根据中文翻译的不同,建造者模式又可以称为生成器模式
<?php
/**
* 抽象建造者(Builder)角色
*
* @author Luffy (lufei@swoole.com)
* @date 2019/9/23
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Builder;
interface Builder
{
// public function createCar();
//创建部件A 比如创建汽车发动机
public function buildPartA();
//创建部件B 比如创建汽车底盘
public function buildPartB();
//创建部件C 比如创建汽车变速箱
public function buildPartC();
// 其他的一些零件
public function buildOthers();
/**
* 返回最后组装成品结果 (返回最后装配好的汽车),
* 成品的组装过程不在这里进行,而是转移到下面的Director类中进行,
* 从而实现了解耦过程和部件。
* return Product
*/
public function getCar();
}
<?php
/**
* 产品(Product)角色
*
* @author Luffy (lufei@swoole.com)
* @date 2019/9/23
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Builder;
class Car
{
/**
* @var array
*/
protected $_parts = [];
public function setPartA()
{
$this->_parts[] = "发动机、";
return $this;
}
public function setPartB()
{
$this->_parts[] = "底盘、";
return $this;
}
public function setPartC()
{
$this->_parts[] = "变速箱、";
return $this;
}
public function setOthers()
{
$this->_parts[] ="其他零件";
return $this;
}
public function getCar()
{
$str = "这辆车由:";
foreach ($this->_parts as $item) {
$str .= $item;
}
$str .= "组成\n";
echo $str;
}
}
<?php
/**
* 具体建造者(CarBuilder)角色
*
* @author Luffy (lufei@swoole.com)
* @date 2019/9/23
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Builder;
class CarBuilder implements Builder
{
protected $car;
public function __construct()
{
$this->car = new Car();
}
public function buildPartA()
{
$this->car->setPartA();
}
public function buildPartB()
{
$this->car->setPartB();
}
public function buildPartC()
{
$this->car->setPartC();
}
public function buildOthers()
{
$this->car->setOthers();
}
public function getCar()
{
return $this->car;
}
}
<?php
/**
* 导演者(Director)角色
*
* @author Luffy (lufei@swoole.com)
* @date 2019/9/23
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Builder;
class Director
{
private $builder;
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
public function build()
{
$this->builder->buildPartA();
$this->builder->buildPartB();
$this->builder->buildPartC();
$this->builder->buildOthers();
return $this->builder->getCar();
}
}
<?php
/**
* 建造者客户端测试文件
*
* @author Luffy (lufei@swoole.com)
* @date 2019/9/23
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
use Luffy\DesignPatterns\Builder\CarBuilder;
use Luffy\DesignPatterns\Builder\Director;
use Luffy\DesignPatterns\Builder\Car;
include __DIR__ . '/../vendor/autoload.php';
class Client
{
public static function index()
{
$carBuilder = new CarBuilder();
$director = new Director($carBuilder);
$newCar = $director->build();
$newCar->getCar();
}
}
// 使用建造者
Client::index();
<?php
/**
* 产品(Product)角色
*
* @author Luffy (lufei@swoole.com)
* @date 2019/9/23
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Builder;
class Car
{
/**
* @var array
*/
protected $_parts = [];
public function setPartA()
{
$this->_parts[] = "发动机、";
return $this;
}
public function setPartB()
{
$this->_parts[] = "底盘、";
return $this;
}
public function setPartC()
{
$this->_parts[] = "变速箱、";
return $this;
}
public function setOthers()
{
$this->_parts[] ="其他零件";
return $this;
}
public function getCar()
{
$str = "这辆车由:";
foreach ($this->_parts as $item) {
$str .= $item;
}
$str .= "组成\n";
echo $str;
}
}
<?php
/**
* 建造者客户端测试文件
*
* @author Luffy (lufei@swoole.com)
* @date 2019/9/23
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
use Luffy\DesignPatterns\Builder\CarBuilder;
use Luffy\DesignPatterns\Builder\Director;
use Luffy\DesignPatterns\Builder\Car;
include __DIR__ . '/../vendor/autoload.php';
class Client
{
public static function test()
{
$car = new Car();
$car->setPartA()
->setPartB()
->setPartC()
->setOthers()
->getCar();
}
}
// 流式操作
Client::test();