mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-01 12:40:11 +02:00
added abstract factory and factory pattern
This commit is contained in:
40
AbstractFactory/AbstractFactory.php
Normal file
40
AbstractFactory/AbstractFactory.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract Factory pattern
|
||||||
|
*
|
||||||
|
* Purpose:
|
||||||
|
* to create series of related or dependant objects without specifying their concrete classes,
|
||||||
|
* usually created classes would all implement the same interface
|
||||||
|
*
|
||||||
|
* Examples:
|
||||||
|
* - A Factory to create media in a CMS: classes would be text, audio, video, picture
|
||||||
|
* - SQL Factory (types are all strings with SQL, but they vary in detail (tables, fields, etc.))
|
||||||
|
* - Zend Framework: Zend_Form::createElement() creates form elements, but you could also call new TextElement() or so
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
abstract class AbstractFactory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @static
|
||||||
|
* @param string $content
|
||||||
|
* @return AbstractFactory\Text
|
||||||
|
*/
|
||||||
|
public static function createText($content)
|
||||||
|
{
|
||||||
|
return new AbstractFactory\Text($content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @static
|
||||||
|
* @param string $path
|
||||||
|
* @param string $name
|
||||||
|
* @return AbstractFactory\Picture
|
||||||
|
*/
|
||||||
|
public static function createPicture($path, $name = '')
|
||||||
|
{
|
||||||
|
return new AbstractFactory\Picture($path, $name);
|
||||||
|
}
|
||||||
|
}
|
15
AbstractFactory/Media.php
Normal file
15
AbstractFactory/Media.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by JetBrains PhpStorm.
|
||||||
|
* User: dominik
|
||||||
|
* Date: 21.08.11
|
||||||
|
* Time: 15:47
|
||||||
|
* To change this template use File | Settings | File Templates.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\AbstractFactory;
|
||||||
|
|
||||||
|
interface Media
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
27
AbstractFactory/Picture.php
Normal file
27
AbstractFactory/Picture.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by JetBrains PhpStorm.
|
||||||
|
* User: dominik
|
||||||
|
* Date: 21.08.11
|
||||||
|
* Time: 15:42
|
||||||
|
* To change this template use File | Settings | File Templates.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\AbstractFactory;
|
||||||
|
|
||||||
|
class Picture implements Media
|
||||||
|
{
|
||||||
|
protected $_path;
|
||||||
|
protected $_name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param string $name
|
||||||
|
*/
|
||||||
|
public function __construct($path, $name = '')
|
||||||
|
{
|
||||||
|
$this->_name = (string) $name;
|
||||||
|
$this->_path = (string) $path;
|
||||||
|
}
|
||||||
|
}
|
24
AbstractFactory/Text.php
Normal file
24
AbstractFactory/Text.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by JetBrains PhpStorm.
|
||||||
|
* User: dominik
|
||||||
|
* Date: 21.08.11
|
||||||
|
* Time: 15:42
|
||||||
|
* To change this template use File | Settings | File Templates.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DesignPatterns\AbstractFactory;
|
||||||
|
|
||||||
|
class Text implements Media
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $_text;
|
||||||
|
|
||||||
|
public function __construct($text)
|
||||||
|
{
|
||||||
|
$this->_text = $text;
|
||||||
|
}
|
||||||
|
}
|
49
FactoryMethod/FactoryMethod.php
Normal file
49
FactoryMethod/FactoryMethod.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory Method pattern
|
||||||
|
*
|
||||||
|
* Purpose:
|
||||||
|
* similar to the AbstractFactory, this pattern is used to create series of related or dependant objects.
|
||||||
|
* The difference between this and the abstract factory pattern is that the factory method 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
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class FactoryMethod
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* the parametrized function to get create an instance
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @return Format
|
||||||
|
*/
|
||||||
|
public static function factory($type)
|
||||||
|
{
|
||||||
|
$className = 'Format' . ucfirst($type);
|
||||||
|
if ( ! class_exists($className)) {
|
||||||
|
throw new Exception('Missing format class.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return new $className();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Formatter
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class FormatString implements Formatter
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class FormatNumber implements Formatter
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user