forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethod.php
More file actions
49 lines (40 loc) · 1.02 KB
/
FactoryMethod.php
File metadata and controls
49 lines (40 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
{
}