PHP建造者模式

Posted wgchen~

tags:

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

场景

  • 创建型模式
  • php建造者模式
  • 简单对象构建复杂对象
  • 基本组件不变,但是组件之间的组合方式善变

示例目录:builder 所有文件在此目录下。

硬件接口 Hardware.php

<?php
namespace builder;

# 硬件接口
interface Hardware{}

软件接口 Software.php

<?php
namespace builder;

# 软件接口
interface Software
{
  public function produce();
}

构建器接口 ProductInterface.php

<?php
namespace builder;

# 构建器接口
Interface ProductInterface
{
  # 硬件构建
  public function hardware();

  # 构建软件
  public function software();
}

操作系统实体 SoftwareOs.php

<?php
namespace builder;

# 操作系统实体
class SoftwareOs implements Software
{
  public function produce($os='android')
  {
    echo "操作系统:" . $os . "\\n";
  }
}

摄像头实体 HardwareCamera.php

<?php
namespace builder;

# 摄像头实体
class HardwareCamera implements Hardware
{
  public function __construct($pixel=32)
  {
    echo "摄像头像素:" . $pixel . "像素\\n";
  }
}

处理器实体 HardwareCpu.php

<?php
namespace builder;

# 处理器实体
class HardwareCpu implements Hardware
{
  public function __construct($quantity=8)
  {
    echo "cpu核心数:" . $quantity . "核\\n";
  }
}

内存实体 HardwareRam.php

<?php
namespace builder;

# 内存实体
class HardwareRam implements Hardware
{
  public function __construct($size=6)
  {
    echo "内存大小:" . $size . "G\\n";
  }
}

屏幕实体 HardwareScreen.php

<?php
namespace builder;

# 屏幕实体
class HardwareScreen implements Hardware
{
  public function __construct($size='5.0')
  {
    echo "屏幕大小:" . $size . "寸\\n";
  }
}

储存实体 HardwareStorage.php

<?php
namespace builder;

# 储存实体
class HardwareStorage implements Hardware
{
  public function __construct($size=32)
  {
    echo "储存大小:" . $size . "G\\n";
  }
}

Mp3构建器 Mp3.php

<?php
namespace builder;

use builder\\ProductInterface;

# Mp3构建器
class Mp3 implements ProductInterface
{
  private $_name = ''; # 名称
  private $_cpu = '';  # 处理器
  private $_ram = '';  # 内存
  private $_storage = '';  # 储存
  private $_os = '';   # 系统

  # 构造函数:$name 名称 - $hardware 构建硬件 - $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']);
  }
}

手机构建器 Phone.php

<?php
namespace builder;

use builder\\ProductInterface;

# 手机构建器
class Phone implements ProductInterface
{
  private $_name = ''; # 名称
  private $_screen = ''; # 屏幕
  private $_cpu = ''; # 处理器
  private $_ram = ''; # 内存
  private $_storage = ''; # 储存
  private $_camera = ''; # 相机
  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']);
  }
}

产品构建器 ProductBuilder.php

<?php
namespace builder;

use builder\\Product;

# 产品构建器
class ProductBuilder
{
  # 参数
  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;
  }
}

产品类 Product.php

<?php
namespace builder;

use builder\\Hardware;
use builder\\Software;

# 产品类
class Product
{
  private $name = '';  # 名称
  private $hardwares = 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 test.php

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

test.php

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

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

/************************************* 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";

PS D:\\PHPstudyWWW\\builder> php .\\test.php

某族MP3 配置如下:
cpu核心数:1核
内存大小:1G
储存大小:128G

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

PS D:\\PHPstudyWWW\\builder> php .\\test.php

某米8s 配置如下:
屏幕大小:5.8寸
摄像头像素:2600w像素
cpu核心数:4核
内存大小:8G
储存大小:128G

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

建造者模式 - 设计模式 - PHP版

PHP建造者模式

php设计模式三-----建造者模式

php实现生成器模式

设计模式建造者模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

大话设计模式-建造者模式