PHP 工廠模式介紹


工廠模式,顧名思義,如同工廠一樣,你把原材料放入工廠中,出來的是成品,而你並不需要知道工廠里做了什么,工廠模式主要用於解耦。個人認為設計模式只能在實戰中更好的理解,當前水平有限,歡迎大家交流

簡單工廠模式

把對象的創建和使用的過程分開,比如: ClassA 調用 ClassB,那么 ClassA 只調用ClassB 的方法,至於實例化 ClassB 則在工廠內實現。這樣既減少了代碼的重復使用,也方便對 ClassB de 后期維護。如果 ClassB 實例化過程很復雜,使用簡單工廠模式就會發現外部無需關注復雜的實例化,只管調用 ClassB 的方法即可,減少錯誤

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

        if ($production instanceof Production) {
            echo 'Nice';
        }
    }
}

工廠方法模式

主要用於限制類的公用方法

<?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.
    }
}

抽象工廠模式

抽象工廠模式 = 工廠方法模式+簡易工廠模式

<?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->price = $price;
        $this->discount = $discount;
    }

    /**
     * @return int
     */
    public function calculatePrice(): int
    {
        return $this->price * $this->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->price = $price;
    }

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

靜態工廠方法

靜態方法主要用於構建相同類型的對象

<?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.
    }
}

四種工廠模式的優缺點

待補充


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM