account_circle Toanph
date_range 15/06/2017
Static Factory
Purpose
Similar to the AbstractFactory, this pattern is used to create series of related or dependent objects.
The difference between this and the 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.
Examples
- Zend Framework: Zend_Cache_Backend or _Frontend use a factory method create cache backends or frontends
UML Diagram

Code
StaticFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | <?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('Unknown format given');
}
}
|
| <?php
namespace DesignPatterns\Creational\StaticFactory;
interface FormatterInterface
{
}
|
| <?php
namespace DesignPatterns\Creational\StaticFactory;
class FormatString implements FormatterInterface
{
}
|
| <?php
namespace DesignPatterns\Creational\StaticFactory;
class FormatNumber implements FormatterInterface
{
}
|
Test
Tests/StaticFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 | <?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');
}
}
|