1
1
<?php
2
+ // veersion 1
3
+ // abstract class Flyweight
4
+ // {
5
+ // abstract public function operation($extringsicState);
6
+ // }
2
7
3
- abstract class Flyweight
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
+ // $state = 22;
43
+ // $f = new FlyweightFactory();
44
+ // $fx = $f->getFlyweight('x');
45
+ // $fx->operation(--$state);
46
+
47
+ // $fx = $f->getFlyweight('y');
48
+ // $fx->operation(--$state);
49
+
50
+ // $fx = $f->getFlyweight('z');
51
+ // $fx->operation(--$state);
52
+
53
+ // $uf = new UnsharedConcreteFlyweight();
54
+ // $uf->operation(--$state);
55
+
56
+ // veersion 2
57
+ // abstract class WebSite
58
+ // {
59
+ // abstract public function use();
60
+ // }
61
+
62
+ // // 具体网站类
63
+ // class ConcreteWebSite extends WebSite
64
+ // {
65
+ // private $name = '';
66
+
67
+ // function __construct($name)
68
+ // {
69
+ // $this->name = $name;
70
+ // }
71
+
72
+ // public function use()
73
+ // {
74
+ // echo "网站分类: ".$this->name."\n";
75
+ // }
76
+ // }
77
+
78
+ // //网站工厂
79
+ // class WebSiteFactory
80
+ // {
81
+ // private $flyweights = [];
82
+
83
+ // public function getWebSiteGategory($key)
84
+ // {
85
+ // if (empty($this->flyweights[$key])) {
86
+ // $this->flyweights[$key] = new ConcreteWebSite($key);
87
+ // }
88
+ // return $this->flyweights[$key];
89
+ // }
90
+
91
+
92
+ // public function getWebSiteCount()
93
+ // {
94
+ // return count($this->flyweights);
95
+ // }
96
+ // }
97
+
98
+
99
+ // $f = new WebSiteFactory();
100
+ // $fx = $f->getWebSiteGategory('产品展示');
101
+ // $fx->use();
102
+
103
+ // $fy = $f->getWebSiteGategory('产品展示');
104
+ // $fy->use();
105
+
106
+ // $fz = $f->getWebSiteGategory('产品展示');
107
+ // $fz->use();
108
+
109
+ // $fl = $f->getWebSiteGategory('博客');
110
+ // $fl->use();
111
+
112
+ // $fm = $f->getWebSiteGategory('博客');
113
+ // $fm->use();
114
+
115
+ // $fn = $f->getWebSiteGategory('博客');
116
+ // $fn->use();
117
+
118
+ // echo "网站分类总数:".$f->getWebSiteCount();
119
+
120
+
121
+ // veersion 3
122
+ class User
4
123
{
5
- abstract public function operation ($ extringsicState );
124
+ private $ name ;
125
+ function __construct ($ name )
126
+ {
127
+ $ this ->name = $ name ;
128
+ }
129
+
130
+ public function getName ()
131
+ {
132
+ return $ this ->name ;
133
+ }
6
134
}
7
135
8
- class ConcreteFlyweight extends Flyweight
136
+ abstract class WebSite
9
137
{
10
- public function operation ($ extringsicState )
11
- {
12
- echo "ConcreteFlyweight: " .$ extringsicState ."\n" ;
13
- }
138
+ abstract public function use (User $ user );
14
139
}
15
140
16
- class UnsharedConcreteFlyweight extends Flyweight
141
+ // 具体网站类
142
+ class ConcreteWebSite extends WebSite
17
143
{
18
- public function operation ($ extringsicState )
19
- {
20
- echo "Unshared ConcreteFlyweight: " .$ extringsicState ."\n" ;
21
- }
144
+ private $ name = '' ;
145
+
146
+ function __construct ($ name )
147
+ {
148
+ $ this ->name = $ name ;
149
+ }
150
+
151
+ public function use (User $ user )
152
+ {
153
+ echo "网站分类: " .$ this ->name ."用户: " .$ user ->getName ()."\n" ;
154
+ }
22
155
}
23
156
24
- class FlyweightFactory
157
+ //网站工厂
158
+ class WebSiteFactory
25
159
{
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
- }
160
+ private $ flyweights = [];
161
+
162
+ public function getWebSiteGategory ($ key )
163
+ {
164
+ if (empty ($ this ->flyweights [$ key ])) {
165
+ $ this ->flyweights [$ key ] = new ConcreteWebSite ($ key );
166
+ }
167
+ return $ this ->flyweights [$ key ];
168
+ }
169
+
170
+
171
+ public function getWebSiteCount ()
172
+ {
173
+ return count ($ this ->flyweights );
174
+ }
39
175
}
40
176
41
- //client
177
+ $ f = new WebSiteFactory ();
178
+ $ fx = $ f ->getWebSiteGategory ('产品展示 ' );
179
+ $ fx ->use (new User ('张伟 ' ));
180
+
181
+ $ fy = $ f ->getWebSiteGategory ('产品展示 ' );
182
+ $ fy ->use (new User ('王伟 ' ));
183
+
184
+ $ fz = $ f ->getWebSiteGategory ('产品展示 ' );
185
+ $ fz ->use (new User ('王芳 ' ));
42
186
43
- $ state = 22 ;
44
- $ f = new FlyweightFactory ();
45
- $ fx = $ f ->getFlyweight ('x ' );
46
- $ fx ->operation (--$ state );
187
+ $ fl = $ f ->getWebSiteGategory ('博客 ' );
188
+ $ fl ->use (new User ('李伟 ' ));
47
189
48
- $ fx = $ f ->getFlyweight ( ' y ' );
49
- $ fx -> operation (-- $ state );
190
+ $ fm = $ f ->getWebSiteGategory ( ' 博客 ' );
191
+ $ fm -> use ( new User ( ' 王秀英 ' ) );
50
192
51
- $ fx = $ f ->getFlyweight ( ' z ' );
52
- $ fx -> operation (-- $ state );
193
+ $ fn = $ f ->getWebSiteGategory ( ' 博客 ' );
194
+ $ fn -> use ( new User ( ' 李秀英 ' ) );
53
195
54
- $ uf = new UnsharedConcreteFlyweight ();
55
- $ uf ->operation (--$ state );
196
+ echo "网站分类总数: " .$ f ->getWebSiteCount ();
0 commit comments