start a restructure

This commit is contained in:
Antonio Spinelli
2014-03-21 18:03:44 -03:00
parent b0b0d4a1a4
commit e59d70a0ac
180 changed files with 21 additions and 16 deletions

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\StaticFactory;
/**
* Class FormatNumber
*/
class FormatNumber implements FormatterInterface
{
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\StaticFactory;
/**
* Class FormatString
*/
class FormatString implements FormatterInterface
{
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\StaticFactory;
/**
* Class FormatterInterface
*/
interface FormatterInterface
{
}

View 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

View 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();
}
}

View 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);
}
}