mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-25 17:21:19 +02:00
28 lines
679 B
PHP
28 lines
679 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
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 Formatter
|
|
*/
|
|
public static function factory(string $type): Formatter
|
|
{
|
|
if ($type == 'number') {
|
|
return new FormatNumber();
|
|
} elseif ($type == 'string') {
|
|
return new FormatString();
|
|
}
|
|
|
|
throw new \InvalidArgumentException('Unknown format given');
|
|
}
|
|
}
|