File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ abstract class Flyweight
4
+ {
5
+ abstract public function operation ($ extringsicState );
6
+ }
7
+
8
+ class ConcreteFlyweight extends Flyweight
9
+ {
10
+ public function operation ($ extringsicState )
11
+ {
12
+ echo "ConcreteFlyweight: " .$ extringsicState ."\n" ;
13
+ }
14
+ }
15
+
16
+ class UnsharedConcreteFlyweight extends Flyweight
17
+ {
18
+ public function operation ($ extringsicState )
19
+ {
20
+ echo "Unshared ConcreteFlyweight: " .$ extringsicState ."\n" ;
21
+ }
22
+ }
23
+
24
+ class FlyweightFactory
25
+ {
26
+ private $ flyweights = [];
27
+
28
+ function __construct ()
29
+ {
30
+ $ this ->flyweights ['x ' ] = new ConcreteFlyweight ();
31
+ $ this ->flyweights ['y ' ] = new ConcreteFlyweight ();
32
+ $ this ->flyweights ['z ' ] = new ConcreteFlyweight ();
33
+ }
34
+
35
+ public function getFlyweight ($ key )
36
+ {
37
+ return $ this ->flyweights [$ key ];
38
+ }
39
+ }
40
+
41
+ //client
42
+
43
+ $ state = 22 ;
44
+ $ f = new FlyweightFactory ();
45
+ $ fx = $ f ->getFlyweight ('x ' );
46
+ $ fx ->operation (--$ state );
47
+
48
+ $ fx = $ f ->getFlyweight ('y ' );
49
+ $ fx ->operation (--$ state );
50
+
51
+ $ fx = $ f ->getFlyweight ('z ' );
52
+ $ fx ->operation (--$ state );
53
+
54
+ $ uf = new UnsharedConcreteFlyweight ();
55
+ $ uf ->operation (--$ state );
You can’t perform that action at this time.
0 commit comments