File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -12,12 +12,14 @@ class FlyweightFactory
12
12
{
13
13
/**
14
14
* Associative store for flyweight objects
15
+ *
15
16
* @var Array
16
17
*/
17
18
private $ pool = array ();
18
19
19
20
/**
20
21
* Magic getter
22
+ *
21
23
* @param string $name
22
24
* @return Flyweight
23
25
*/
@@ -29,6 +31,9 @@ public function __get($name)
29
31
return $ this ->pool [$ name ];
30
32
}
31
33
34
+ /**
35
+ * @return int
36
+ */
32
37
public function totalNumber ()
33
38
{
34
39
return sizeof ($ this ->pool );
Original file line number Diff line number Diff line change
1
+ `Flyweight `__
2
+ ==========
3
+
4
+ Purpose
5
+ -------
6
+
7
+ To minimise memory usage, a Flyweight shares as much as possible memory with similar objects. It
8
+ is needed when a large amount of objects is used that don't differ much in state. A common practice is
9
+ to hold state in external data structures and pass them to the flyweight object when needed.
10
+
11
+ UML Diagram
12
+ -----------
13
+
14
+ .. image :: uml/uml.png
15
+ :alt: Alt Facade UML Diagram
16
+ :align: center
17
+
18
+ Code
19
+ ----
20
+
21
+ You can also find these code on `GitHub `_
22
+
23
+ FlyweightInterface.php
24
+
25
+ .. literalinclude :: FlyweightInterface.php
26
+ :language: php
27
+ :linenos:
28
+
29
+ CharacterFlyweight.php
30
+
31
+ .. literalinclude :: CharacterFlyweight.php
32
+ :language: php
33
+ :linenos:
34
+
35
+ FlyweightFactory.php
36
+
37
+ .. literalinclude :: FlyweightFactory.php
38
+ :language: php
39
+ :linenos:
40
+
41
+ Test
42
+ ----
43
+
44
+ Tests/FlyweightTest.php
45
+
46
+ .. literalinclude :: Tests/FlyweightTest.php
47
+ :language: php
48
+ :linenos:
49
+
50
+ .. _`GitHub` : https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/Flyweight
51
+ .. __ : https://en.wikipedia.org/wiki/Flyweight_pattern
You can’t perform that action at this time.
0 commit comments