mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-10 17:04:01 +02:00
start a restructure
This commit is contained in:
11
Creational/StaticFactory/FormatNumber.php
Normal file
11
Creational/StaticFactory/FormatNumber.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\StaticFactory;
|
||||
|
||||
/**
|
||||
* Class FormatNumber
|
||||
*/
|
||||
class FormatNumber implements FormatterInterface
|
||||
{
|
||||
|
||||
}
|
11
Creational/StaticFactory/FormatString.php
Normal file
11
Creational/StaticFactory/FormatString.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\StaticFactory;
|
||||
|
||||
/**
|
||||
* Class FormatString
|
||||
*/
|
||||
class FormatString implements FormatterInterface
|
||||
{
|
||||
|
||||
}
|
11
Creational/StaticFactory/FormatterInterface.php
Normal file
11
Creational/StaticFactory/FormatterInterface.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\StaticFactory;
|
||||
|
||||
/**
|
||||
* Class FormatterInterface
|
||||
*/
|
||||
interface FormatterInterface
|
||||
{
|
||||
|
||||
}
|
11
Creational/StaticFactory/README.md
Normal file
11
Creational/StaticFactory/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# 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
|
31
Creational/StaticFactory/StaticFactory.php
Normal file
31
Creational/StaticFactory/StaticFactory.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\StaticFactory;
|
||||
|
||||
/**
|
||||
* Note1: Remember, static => global => evil
|
||||
* Note2: Cannot be subclassed or mock-upped or have multiple different instances
|
||||
*/
|
||||
class StaticFactory
|
||||
{
|
||||
/**
|
||||
* the parametrized function to get create an instance
|
||||
*
|
||||
* @param string $type
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return FormatterInterface
|
||||
*/
|
||||
public static function factory($type)
|
||||
{
|
||||
$className = __NAMESPACE__ . '\Format' . ucfirst($type);
|
||||
|
||||
if (!class_exists($className)) {
|
||||
throw new \InvalidArgumentException('Missing format class.');
|
||||
}
|
||||
|
||||
return new $className();
|
||||
}
|
||||
}
|
30
Creational/StaticFactory/Test/StaticFactoryTest.php
Normal file
30
Creational/StaticFactory/Test/StaticFactoryTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Tests\StaticFactory;
|
||||
|
||||
use DesignPatterns\StaticFactory\StaticFactory;
|
||||
|
||||
/**
|
||||
* Tests for Static Factory pattern
|
||||
*
|
||||
*/
|
||||
class StaticFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function getTypeList()
|
||||
{
|
||||
return array(
|
||||
array('string'),
|
||||
array('number')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getTypeList
|
||||
*/
|
||||
public function testCreation($type)
|
||||
{
|
||||
$obj = StaticFactory::factory($type);
|
||||
$this->assertInstanceOf('DesignPatterns\StaticFactory\FormatterInterface', $obj);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user