宇辰时空 發表於 2019-6-22 17:37:00

PHP 工厂模式介绍

<blockquote>
<p>工厂模式,顾名思义,如同工厂一样,你把原材料放入工厂中,出来的是成品,而你并不需要知道工厂里做了什么,工厂模式主要用于解耦。个人认为设计模式只能在实战中更好的理解,当前水平有限,欢迎大家交流</p>
</blockquote>
<h2 id="简单工厂模式">简单工厂模式</h2>
<blockquote>
<p>把对象的创建和使用的过程分开,比如: ClassA 调用 ClassB,那么 ClassA 只调用ClassB的方法,至于实例化 ClassB 则在工厂内实现。这样既减少了代码的重复使用,也方便对 ClassB de 后期维护。如果 ClassB 实例化过程很复杂,使用简单工厂模式就会发现外部无需关注复杂的实例化,只管调用 ClassB 的方法即可,减少错误</p>
</blockquote>
<blockquote></blockquote>
<p><img src="https://img2018.cnblogs.com/blog/1410693/201906/1410693-20190622173430468-241356379.png"></p>
<pre><code class="language-php">&lt;?php
namespace Factory\SimpleFactory;


class SimpleFactory
{
    public function createProduction(): Production
    {
      return new Production();
    }
}

class Production
{
    public function getPrice(int $price)
    {
      return $price * 2;
    }
}

class Test
{
    public function __construct()
    {
      $factory = new SimpleFactory();
      $production = $factory-&gt;createProduction();

      if ($production instanceof Production) {
            echo 'Nice';
      }
    }
}
</code></pre>
<h2 id="工厂方法模式">工厂方法模式</h2>
<blockquote>
<p>主要用于限制类的公用方法<br>
<img src="https://img2018.cnblogs.com/blog/1410693/201906/1410693-20190622173343088-1305909634.png"></p>
</blockquote>
<pre><code class="language-php">&lt;?php


namespace Factory\SimpleFactory;


/**
* Interface FunctionFactory
* @package Factory\SimpleFactory
*/
interface FunctionFactory
{
    /**
   * @param array $data
   * @return array
   */
    public function create(array $data);

    /**
   * @param int $id
   * @return bool
   */
    public function delete(int $id);

    /**
   * @param array $data
   * @return array
   */
    public function update(array $data);

    /**
   * @return array
   */
    public function select();
}

class ProductionRepository implements FunctionFactory
{
    public function create(array $data)
    {
      // TODO: Implement create() method.
    }

    public function delete(int $id)
    {
      // TODO: Implement delete() method.
    }

    public function update(array $data)
    {
      // TODO: Implement update() method.
    }

    public function select()
    {
      // TODO: Implement select() method.
    }
}
</code></pre>
<h2 id="抽象工厂模式">抽象工厂模式</h2>
<blockquote>
<p>抽象工厂模式 = 工厂方法模式+简易工厂模式<br>
<img src="https://img2018.cnblogs.com/blog/1410693/201906/1410693-20190622173520962-1080843204.png"></p>
</blockquote>
<pre><code class="language-php">&lt;?php

namespace Factory\SimpleFactory;


/**
* Class AbstractFactory
* @package Factory\SimpleFactory
*/
class AbstractFactory
{
    /**
   * @param int $price
   * @param int $discount
   * @return PromotionPhoneProduct
   */
    public function createPromotionPhoneProduct(int $price, int $discount)
    {
      return new PromotionPhoneProduct($price, $discount);
    }

    /**
   * @param int $price
   * @return PhoneProduct
   */
    public function createPhoneProduct(int $price)
    {
      return new PhoneProduct($price);
    }
}

/**
* Interface Product
* @package Factory\SimpleFactory
*/
interface Product
{
    /**
   * @return int
   */
    public function calculatePrice(): int;
}

/**
* Class PhoneProduct
* @package Factory\SimpleFactory
*/
class PromotionPhoneProduct implements Product
{
    /**
   * @var int
   */
    private $price;

    /**
   * @var int
   */
    private $discount;

    /**
   * PhoneProduct constructor.
   * @param int $price
   * @param int $discount
   */
    public function __construct(int $price, int $discount)
    {
      $this-&gt;price = $price;
      $this-&gt;discount = $discount;
    }

    /**
   * @return int
   */
    public function calculatePrice(): int
    {
      return $this-&gt;price * $this-&gt;discount;
    }
}

/**
* Class PhoneProduct
* @package Factory\SimpleFactory
*/
class PhoneProduct implements Product
{
    /**
   * @var int
   */
    private $price;

    /**
   * PhoneProduct constructor.
   * @param int $price
   * @param
   */
    public function __construct(int $price)
    {
      $this-&gt;price = $price;
    }

    /**
   * @return int
   */
    public function calculatePrice(): int
    {
      return $this-&gt;price;
    }
}
</code></pre>
<h2 id="静态工厂方法">静态工厂方法</h2>
<blockquote>
<p>静态方法主要用于构建相同类型的对象<br>
<img src="https://img2018.cnblogs.com/blog/1410693/201906/1410693-20190622173610079-1410357428.png"></p>
</blockquote>
<pre><code class="language-php">&lt;?php


namespace Factory\SimpleFactory;


/**
* Class StaticFactory
* @package Factory\SimpleFactory
*/
class StaticFactory
{
    /**
   * @param string $type
   * @return NumericClass|StringClass
   */
    public static function build(string $type)
    {
      switch ($type) {
            case 'string':
                return new StringClass();
                break;
            case 'numeric':
                return new NumericClass();
            default:
                break;
      }
    }
}

/**
* Interface TypeInterface
* @package Factory\SimpleFactory
*/
interface TypeInterface
{
    /**
   * @return mixed
   */
    public function getType();
}

/**
* Class NumericClass
* @package Factory\SimpleFactory
*/
class NumericClass implements TypeInterface
{
    /**
   * @return mixed|void
   */
    public function getType()
    {
      // TODO: Implement getType() method.
    }
}

/**
* Class StringClass
* @package Factory\SimpleFactory
*/
class StringClass implements TypeInterface
{
    /**
   * @return mixed|void
   */
    public function getType()
    {
      // TODO: Implement getType() method.
    }
}
</code></pre>
<h2 id="四种工厂模式的优缺点">四种工厂模式的优缺点</h2>
<blockquote>
<p>待补充</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/it-abel/p/11031845.html
頁: [1]
查看完整版本: PHP 工厂模式介绍