Development Blog
Collection of development articles I've put together...
Static Factory Pattern
Similar to the AbstractFactory, this pattern is used to create series of related or dependent objects.
2
Likes
Stuart Todd
•
2 years ago
•
Design Patterns: Creational
Purpose
The purpose of the design pattern
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, build or make.
Examples
Examples of how the design pattern can be used
UML
UML design pattern diagram
Code
Code snippets
Formatter
Formatter Interface that all concrete classes (FormatNumber and FormatText) must adhere to. Basically ensures they have a format method otherwise an error is thrown.
namespace DesignPatterns\Creational\StaticFactory;
interface Formatter
{
public function format(string $input): string;
}
FormatNumber
NumberFormat concrete class which implements the Formatter Interface.
namespace DesignPatterns\Creational\StaticFactory;
class FormatNumber implements Formatter
{
public function format(string $input): string
{
return number_format((int) $input);
}
}
FormatString
NumberFormat concrete class which implements the Formatter Interface.
namespace DesignPatterns\Creational\StaticFactory;
class FormatString implements Formatter
{
public function format(string $input): string
{
return $input;
}
}
StaticFactory
The StaticFactory class is called which returns the appropriate class.
i.e StaticFactory::factory('string')->format('This is an example');
Using this approach stops the class calling the above being open to modification (only the StaticFactory would be open). You could defer to a config at this point, closing modification to the StaticFactory as well.
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
{
if ($type == 'number') {
return new FormatNumber();
} elseif ($type == 'string') {
return new FormatString();
}
throw new InvalidArgumentException('Unknown format given');
}
}