mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-28 10:40:17 +02:00
24 lines
619 B
PHP
24 lines
619 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Creational\StaticFactory;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
public static function factory(string $type): Formatter
|
|
{
|
|
return match ($type) {
|
|
'number' => new FormatNumber(),
|
|
'string' => new FormatString(),
|
|
default => throw new InvalidArgumentException('Unknown format given'),
|
|
};
|
|
}
|
|
}
|