建造者模式

77 阅读2分钟

 创建型模式
php建造者模式
简单对象构建复杂对象
基本组件不变,但是组件之间的组合方式善变
下面我们来构建手机和mp3
手机简单由以下构成
手机 => 名称,硬件, 软件
硬件又由以下硬件构成
硬件 => 屏幕,cpu, 内存, 储存, 摄像头
软件又由以下构成
软件 => android, ubuntu

mp3简单由以下构成
手机 => 名称,硬件, 软件
硬件又由以下硬件构成
硬件 => cpu, 内存, 储存
软件又由以下构成
软件 => mp3 os

<?php
namespace builder;

/**
 * 硬件接口
 */
interface Hardware
{
}
<?php
namespace builder;

/**
 * 摄像头实体
 */
class HardwareCamera implements Hardware
{
  public function __construct($pixel=32)
  {
    echo "摄像头像素:" . $pixel . "像素\n";
  }
}
<?php
namespace builder;

/**
 * 处理器实体
 */
class HardwareCpu implements Hardware
{
  public function __construct($quantity=8)
  {
    echo "cpu核心数:" . $quantity . "核\n";
  }
}
<?php
namespace builder;

/**
 * 内存实体
 */
class HardwareRam implements Hardware
{
  public function __construct($size=6)
  {
    echo "内存大小:" . $size . "G\n";
  }
}
<?php
namespace builder;

/**
 * 屏幕实体
 */
class HardwareScreen implements Hardware
{
  public function __construct($size='5.0')
  {
    echo "屏幕大小:" . $size . "寸\n";
  }
}
<?php
namespace builder;

/**
 * 储存实体
 */
class HardwareStorage implements Hardware
{
  public function __construct($size=32)
  {
    echo "储存大小:" . $size . "G\n";
  }
}
<?php
namespace builder;

/**
 * 软件接口
 */
interface Software
{
  public function produce();
}
<?php
namespace builder;

/**
 * 操作系统实体
 */
class SoftwareOs implements Software
{
  public function produce($os='android')
  {
    echo "操作系统:" . $os . "\n";
  }
}
<?php
namespace builder;

/**
 * 构建器接口
 */
Interface ProductInterface
{
  /**
   * 硬件构建
   * @return void
   */
  public function hardware();

  /**
   * 构建软件
   * @return void
   */
  public function software();
}
<?php
namespace builder;

use builder\ProductInterface;

/**
 * Mp3构建器
 */
class Mp3 implements ProductInterface
{
  /**
   * 名称
   * @var string
   */
  private $_name = '';

  /**
   * 处理器
   * @var string
   */
  private $_cpu = '';

  /**
   * 内存
   * @var string
   */
  private $_ram = '';

  /**
   * 储存
   * @var string
   */
  private $_storage = '';

  /**
   * 系统
   * @var string
   */
  private $_os = '';

  /**
   * 构造函数
   *
   * @param string $name     名称
   * @param array  $hardware 构建硬件
   * @param array  $software 构建软件
   */
  public function __construct($name='', $hardware=array(), $software=array())
  {
    // 名称
    $this->_name = $name;
    echo $this->_name . " 配置如下:\n";
    // 构建硬件
    $this->hardware($hardware);
    // 构建软件
    $this->software($software);
  }

  /**
   * 构建硬件
   *
   * @param  array  $hardware 硬件参数
   * @return void
   */
  public function hardware($hardware=array())
  {
        // 创建 CPU
        $this->_cpu = new HardwareCpu($hardware['cpu']);

        // 创建内存
        $this->_ram = new HardwareRam($hardware['ram']);

        // 创建存储
        $this->_storage = new HardwareStorage($hardware['storage']);
  }

  /**
   * 构建软件
   * 
   * @param  array  $software 软件参数
   * @return void
   */
  public function software($software=array())
  {
    // 创建操作系统
    $softwareOs     = new SoftwareOs();
    $this->_os      = $softwareOs->produce($software['os']);
  }
}
<?php
namespace builder;

use builder\ProductInterface;

/**
 * 手机构建器
 */
class Phone implements ProductInterface
{
  /**
   * 名称
   * @var string
   */
  private $_name = '';

  /**
   * 屏幕
   * @var string
   */
  private $_screen = '';

  /**
   * 处理器
   * @var string
   */
  private $_cpu = '';

  /**
   * 内存
   * @var string
   */
  private $_ram = '';

  /**
   * 储存
   * @var string
   */
  private $_storage = '';

  /**
   * 相机
   * @var string
   */
  private $_camera = '';

  /**
   * 系统
   * @var string
   */
  private $_os = '';

  /**
   * 构造函数
   * @param string $name     名称
   * @param array  $hardware 构建硬件
   * @param array  $software 构建软件
   */
  public function __construct($name='', $hardware=array(), $software=array())
  {
    // 名称
    $this->_name = $name;
    echo $this->_name . " 配置如下:\n";
    // 构建硬件
    $this->hardware($hardware);
    // 构建软件
    $this->software($software);
  }

  /**
   * 构建硬件
   * @param  array  $hardware 硬件参数
   * @return void
   */
  public function hardware($hardware=array())
  {
        // 创建屏幕
        $this->_screen  = new HardwareScreen($hardware['screen']);
        // 创建cpu
        $this->_cpu     = new HardwareCpu($hardware['cpu']);
        // 创建内存
        $this->_ram     = new HardwareRam($hardware['ram']);
        // 创建储存
        $this->_storage = new HardwareStorage($hardware['storage']);
        // 创建摄像头
        $this->_camera  = new HardwareCamera($hardware['camera']);
  }

  /**
   * 构建软件
   * @param  array  $software 软件参数
   * @return void
   */
  public function software($software=array())
  {
    // 创建操作系统
    $softwareOs     = new SoftwareOs();
    $this->_os      = $softwareOs->produce($software['os']);
  }
}
<?php
namespace builder;

use builder\Hardware;
use builder\Software;

/**
 * 产品类
 */
class Product
{
  /**
   * 名称
   * @var string
   */
  private $name = '';

  /**
   * 硬件
   * @var array
   */
  private $hardwares = array();

  /**
   * 软件
   * @var array
   */
  private $softwares = array();

  /**
   * 构造函数
   *
   * @param string $name 名称
   */
  public function __construct($name='')
  {
    // 名称
    $this->name = $name;
    echo $this->name . " 配置如下:\n";
  }

  /**
   * 构建硬件
   *
   * @param  Hardware  $hardware 硬件参数
   * @return void
   */
  public function addHardware(Hardware $instance)
  {
    $this->hardwares[] = $instance;
  }

  /**
   * 构建软件
   * 
   * @param  Software  $software 软件参数
   * @return void
   */
  public function addSoftware(Software $instance)
  {
    $this->softwares[] = $instance;
  }
}
<?php
namespace builder;

use builder\Product;

/**
 * 产品构建器
 */
class ProductBuilder
{

  /**
   * 参数
   *
   * @var array
   */
  private $params = [
    'name'     => '',
    'hardware' => [],
    'software' => []
  ];

  /**
   * 构造函数
   */
  public function __construct($params = [])
  {
    
  }

  /**
   * mp3
   *
   * @param array $params 参数
   * @return Product Mp3
   */
  public function getMp3($params = [])
  {
    $this->params = $params;
    $mp3 = new Product($this->params['name']);
    $mp3->addHardware(new HardwareCpu($this->params['hardware']['cpu']));
    $mp3->addHardware(new HardwareRam($this->params['hardware']['ram']));
    $mp3->addHardware(new HardwareStorage($this->params['hardware']['storage']));
    $mp3->addSoftware(new SoftwareOs($this->params['software']['os']));
    return $mp3;
  }

  /**
   * phone
   * 
   * @param array $params 参数
   * @return Product Phone
   */
  public function getPhone($params = [])
  {
    $this->params = $params;
    $phone = new Product($this->params['name']);
    $phone->addHardware(new HardwareScreen($this->params['hardware']['screen']));
    $phone->addHardware(new HardwareCamera($this->params['hardware']['camera']));
    $phone->addHardware(new HardwareCpu($this->params['hardware']['cpu']));
    $phone->addHardware(new HardwareRam($this->params['hardware']['ram']));
    $phone->addHardware(new HardwareStorage($this->params['hardware']['storage']));
    $phone->addSoftware(new SoftwareOs($this->params['software']['os']));
    return $phone;
  }
}
<?php
/**
 * 创建型模式
 *
 * php建造者模式
 * 简单对象构建复杂对象
 * 基本组件不变,但是组件之间的组合方式善变
 *
 * 下面我们来构建手机和mp3
 *
 * // 手机简单由以下构成
 * 手机 => 名称,硬件, 软件
 * // 硬件又由以下硬件构成
 * 硬件 => 屏幕,cpu, 内存, 储存, 摄像头
 * // 软件又由以下构成
 * 软件 => android, ubuntu
 *
 * * // mp3简单由以下构成
 * 手机 => 名称,硬件, 软件
 * // 硬件又由以下硬件构成
 * 硬件 => cpu, 内存, 储存
 * // 软件又由以下构成
 * 软件 => mp3 os
 */


// 注册自加载
spl_autoload_register('autoload');

function autoload($class)
{
  require dirname($_SERVER['SCRIPT_FILENAME']) . '//..//' . str_replace('\\', '/', $class) . '.php';
}

/************************************* test *************************************/

use builder\ProductBuilder;

$builder = new ProductBuilder();

// 生产一款mp3
$builder->getMp3([
  'name' => '某族MP3',
  'hardware' => [
    'cpu'     => 1,
    'ram'     => 1,
    'storage' => 128,
  ],
  'software' => ['os' => 'mp3 os']
]);

echo "\n";
echo "----------------\n";
echo "\n";

// 生产一款手机
$builder->getPhone([
  'name' => '某米8s',
  'hardware' => [
    'screen'  => '5.8',
    'camera'  => '2600w',
    'cpu'     => 4,
    'ram'     => 8,
    'storage' => 128,
  ],
  'software' => ['os' => 'android 6.0']
]);