Purpose:

Similiar to the AbstractFactory, this pattern is used to create series of related or dependent objects. The difference between this and abstract factory pattern is that the static factory pattern uses just one static method to create all types of objects it can create. It is usually named factory or build.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 Creational --- Static Factory 随笔

StaticFactory.php

<?php 

namespace DesignPatterns\Creational\StaticFactory;

/**
 * Note1: Remember, static means global state which is evil because it can't be mocked for tests
 * Note2: Cannot be subclassed or mock-upped or have multiple different instances.
 */

final class StaticFactory
{
    /**
        * @param string $type
        *
        * @return FormatterInterface
    */
    public static function factory(string $type): FormatterInterface
    {
        if ($type == 'number') {
            return new FormatNumber();
        }

        if ($type == 'string') {
            return new FormatString();
        }

        throw new \InvalidArgumentException("Unknow format given");
    }
}

?>

  

FormatterInterface.php

<?php

namespace DesignPatterns\Creational\StaticFactory;

interface FormatterInterface
{
}

  

FormatString.php

<?php 

namespace DesignPattern\Creational\StaticFactory;

class FormatString implements FormatterInterface
{
    
}
?>

  

FormatNumber.php

<?php 

namespace DesignPatterns\Creational\StaticFactory;

class FormatNumber implements FormatterInterface
{
    
}
?>

  

Tests/StaticFactoryTest.php

<?php 

namespace DesignPatterns\Creational\StaticFactory\Tests;

use DesignPatterns\Creational\StaticFactory\StaticFactory;
use PHPUnit\Framework\TestCase;

class StaticFactoryTest extends TestCase
{

    public function testCanCreateNumberFormatter()
    {
        $this->assertInstanceOf(
            'DesignPatterns\Creational\StaticFactory\FormatNumber',
            StaticFactory::factory('number')
        );

        public function testCanCreateStringFormatter()
        {
            $this->assertInstanceOf(
                'DesignPatterns\Creational\StaticFactory\FormatString',
                StaticFactory::factory('string')
            );
        }

        /**
         * @expectedException \InvalidArgumentException
         */
        public function testException()
        {
            StaticFactory::factory('object');
        }
    }
}

?>

  

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄