-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPackageFactory.php
More file actions
60 lines (51 loc) · 1.75 KB
/
PackageFactory.php
File metadata and controls
60 lines (51 loc) · 1.75 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
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace DesignPatterns\Structural\Flyweight;
/**
* The factory instantiates flyweights and ensures that flyweights are shared
* properly.
*
* @author Vlad Riabchenko <contact@vria.eu>
*/
class PackageFactory
{
const BIG = 'big';
const SMALL = 'small';
const CUSTOM = 'custom';
private $packages = [];
/**
* When client requests a package this method returns either a shared
* flyweight package or an unshared custom package.
*
* @param $type
* @param null $name
* @param null $volume
*
* @return PackageInterface
*/
public function get($type, $name = null, $volume = null)
{
switch ($type) {
// Returns a single existing big package.
case self::BIG:
// Instantiate a new big package flyweight if does not exist.
if (!array_key_exists(self::BIG, $this->packages)) {
$this->packages[self::BIG] = new BigPackage();
}
return $this->packages[self::BIG];
// Returns a single existing small package.
case self::SMALL:
// Instantiate a new small package flyweight if does not exist.
if (!array_key_exists(self::SMALL, $this->packages)) {
$this->packages[self::SMALL] = new SmallPackage();
}
return $this->packages[self::SMALL];
// Returns a custom package.
case self::CUSTOM:
// It is always a new instance that cannot represent different
// packages.
return new CustomPackage($name, $volume);
}
// Unknown type.
throw new \InvalidArgumentException('Unknown package type');
}
}