mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-04 05:57:25 +02:00
PSR-0 for Static Factory
This commit is contained in:
8
StaticFactory/FormatNumber.php
Normal file
8
StaticFactory/FormatNumber.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\StaticFactory;
|
||||
|
||||
class FormatNumber implements Formatter
|
||||
{
|
||||
|
||||
}
|
8
StaticFactory/FormatString.php
Normal file
8
StaticFactory/FormatString.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\StaticFactory;
|
||||
|
||||
class FormatString implements Formatter
|
||||
{
|
||||
|
||||
}
|
8
StaticFactory/Formatter.php
Normal file
8
StaticFactory/Formatter.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\StaticFactory;
|
||||
|
||||
interface Formatter
|
||||
{
|
||||
|
||||
}
|
@@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns;
|
||||
namespace DesignPatterns\StaticFactory;
|
||||
|
||||
/**
|
||||
* Factory Method pattern
|
||||
* Static Factory pattern
|
||||
*
|
||||
* Purpose:
|
||||
* similar to the AbstractFactory, this pattern is used to create series of related or dependant objects.
|
||||
@@ -13,9 +13,12 @@ namespace DesignPatterns;
|
||||
* Examples:
|
||||
* - Zend Framework: Zend_Cache_Backend or _Frontend use a factory method create cache backends or frontends
|
||||
*
|
||||
* Note1: Remember, static => global => evil
|
||||
* Note2: Cannot be subclassed or mock-uped or have multiple different instances
|
||||
*/
|
||||
class FactoryMethod
|
||||
class StaticFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* the parametrized function to get create an instance
|
||||
*
|
||||
@@ -24,26 +27,13 @@ class FactoryMethod
|
||||
*/
|
||||
public static function factory($type)
|
||||
{
|
||||
$className = 'Format' . ucfirst($type);
|
||||
if ( ! class_exists($className)) {
|
||||
throw new Exception('Missing format class.');
|
||||
$className = __NAMESPACE__ . '\Format' . ucfirst($type);
|
||||
|
||||
if (!class_exists($className)) {
|
||||
throw new \InvalidArgumentException('Missing format class.');
|
||||
}
|
||||
|
||||
return new $className();
|
||||
}
|
||||
}
|
||||
|
||||
interface Formatter
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class FormatString implements Formatter
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class FormatNumber implements Formatter
|
||||
{
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user