forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyweightFactory.php
More file actions
41 lines (36 loc) · 806 Bytes
/
FlyweightFactory.php
File metadata and controls
41 lines (36 loc) · 806 Bytes
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
<?php
namespace DesignPatterns\Structural\Flyweight;
/**
* A factory manages shared flyweights. Clients shouldn't instaniate them directly,
* but let the factory take care of returning existing objects or creating new ones.
*/
class FlyweightFactory
{
/**
* Associative store for flyweight objects.
*
* @var array
*/
private $pool = array();
/**
* Magic getter.
*
* @param string $name
*
* @return Flyweight
*/
public function __get($name)
{
if (!array_key_exists($name, $this->pool)) {
$this->pool[$name] = new CharacterFlyweight($name);
}
return $this->pool[$name];
}
/**
* @return int
*/
public function totalNumber()
{
return count($this->pool);
}
}