8000 complete chapter 26 · sockstack/design-patterns-by-php@d3607ea · GitHub
[go: up one dir, main page]

Skip to content

Commit d3607ea

Browse files
committed
complete chapter 26
1 parent 0815162 commit d3607ea

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

chapter25.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,4 @@ $c2->send('no, do you want to invite me to dinner?');
9999
100100
上一章:[第二十四章 加薪非要老总批 --- 职责链模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter24.md)
101101

102-
下一章:[第二十六章 ](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter26.md)
102+
下一章:[第二十六章 项目多也别傻做 --- 享元模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter26.md)

chapter26.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
### 第二十六章 项目多也别傻做 --- 享元模式
2+
3+
```php
4+
class User
5+
{
6+
private $name;
7+
function __construct($name)
8+
{
9+
$this->name = $name;
10+
}
11+
12+
public function getName()
13+
{
14+
return $this->name;
15+
}
16+
}
17+
18+
abstract class WebSite
19+
{
20+
abstract public function use(User $user);
21+
}
22+
23+
// 具体网站类
24+
class ConcreteWebSite extends WebSite
25+
{
26+
private $name = '';
27+
28+
function __construct($name)
29+
{
30+
$this->name = $name;
31+
}
32+
33+
public function use(User $user)
34+
{
35+
echo "网站分类: ".$this->name."用户:".$user->getName()."\n";
36+
}
37+
}
38+
39+
//网站工厂
40+
class WebSiteFactory
41+
{
42+
private $flyweights = [];
43+
44+
public function getWebSiteGategory($key)
45+
{
46+
if (empty($this->flyweights[$key])) {
47+
$this->flyweights[$key] = new ConcreteWebSite($key);
48+
}
49+
return $this->flyweights[$key];
50+
}
51+
52+
53+
public function getWebSiteCount()
54+
{
55+
return count($this->flyweights);
56+
}
57+
}
58+
59+
$f = new WebSiteFactory();
60+
$fx = $f->getWebSiteGategory('产品展示');
61+
$fx->use(new User('张伟'));
62+
63+
$fy = $f->getWebSiteGategory('产品展示');
64+
$fy->use(new User('王伟'));
65+
66+
$fz = $f->getWebSiteGategory('产品展示');
67+
$fz->use(new User('王芳'));
68+
69+
$fl = $f->getWebSiteGategory('博客');
70+
$fl->use(new User('李伟'));
71+
72+
$fm = $f->getWebSiteGategory('博客');
73+
$fm->use(new User('王秀英'));
74+
75+
$fn = $f->getWebSiteGategory('博客');
76+
$fn->use(new User('李秀英'));
77+
78+
echo "网站分类总数:".$f->getWebSiteCount()."\n";
79+
```
80+
81+
总结
82+
83+
> ***享原模式*** 运用共享技术有效地支持大量细粒度的对象
84+
85+
> 享原模式可以避免大量非常类似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,有时就能够受大幅度地减少需要实例化的类的数量。如果能把参数移到类实例的外面,在方法调用时将它们传递进来,就可以通过共享大幅度地减少单个实例的数目。
86+
87+
> 如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就应该考虑使用;还有就是对象的大多数状态可以外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象,此时可以考虑使用享原模式。
88+
89+
上一章:[第二十五章 世界需要和平 --- 中介者模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter25.md)
90+
91+
下一章:[第二十七章 ](https://github.com/flyingalex/design-patterns-by-php/blob/master/chapter27.md)

0 commit comments

Comments
 (0)
0