Skip to content

Commit

Permalink
PSR-0 for Static Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Trismegiste committed May 10, 2013
1 parent 7ca8f61 commit 2bea1f3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
8 changes: 8 additions & 0 deletions StaticFactory/FormatNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace DesignPatterns\StaticFactory;

class FormatNumber implements Formatter
{

}
8 changes: 8 additions & 0 deletions StaticFactory/FormatString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace DesignPatterns\StaticFactory;

class FormatString implements Formatter
{

}
8 changes: 8 additions & 0 deletions StaticFactory/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace DesignPatterns\StaticFactory;

interface Formatter
{

}
30 changes: 10 additions & 20 deletions StaticFactory/StaticFactory.php
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -13,9 +13,12 @@
* 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
*
Expand All @@ -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
{

}

0 comments on commit 2bea1f3

Please sign in to comment.