10000 add hugo book · gistol/design-patterns-by-php@6dc3a97 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6dc3a97

Browse files
author
flyingalex
committed
add hugo book
1 parent e0bdd9f commit 6dc3a97

File tree

164 files changed

+27232
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+27232
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "book/themes/book"]
2+
path = book/themes/book
3+
url = https://github.com/alex-shpak/hugo-book

book/.DS_Store

6 KB
Binary file not shown.

book/archetypes/default.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: "{{ replace .Name "-" " " | title }}"
3+
date: {{ .Date }}
4+
draft: true
5+
---
6+

book/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
baseURL: "http://example.org/"
2+
languageCode: "en-us"
3+
title: "design-patterns-by-php"
4+
theme: "book"

book/content/.DS_Store

6 KB
Binary file not shown.

book/content/_index.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: 序言
3+
type: docs
4+
---
5+
6+
[《大话设计模式》](https://book.douban.com/subject/2334288/)php版本
7+
用自己现在用的语言重新写一遍,文中的代码均可以在终端内执行
8+
9+
相关资源:
10+
11+
[Design Patterns for Humans](https://github.com/kamranahmedse/design-patterns-for-humans)
12+
13+
[DesignPatternsPHP](https://github.com/domnikl/DesignPatternsPHP)
14+
## 目录
15+
16+
- [第一章 代码无错就是优 ---简单工厂模式]({{< relref "/docs/chapter1" >}})
17+
18+
- [第二章 商场促销 --- 策略模式]({{< relref "/docs/chapter2" >}})
19+
20+
- [第三章 拍摄UFO --- 单一职责原则]({{< relref "/docs/chapter3" >}})
21+
22+
- [第四章 研求职两不误 --- 开放-封闭原则]({{< relref "/docs/chapter4" >}})
23+
24+
- [第五章 会修电脑不会修收音机? --- 依赖倒转原则]({{< relref "/docs/chapter5" >}})
25+
26+
- [第六章 穿什么有这么重要吗 --- 装饰模式]({{< relref "/docs/chapter6" >}})
27+
28+
- [第七章 为别人做嫁衣 --- 代理模式]({{< relref "/docs/chapter7" >}})
29+
30+
- [第八章 雷锋依然在人间 --- 工厂方法模式]({{< relref "/docs/chapter8" >}})
31+
32+
- [第九章 简历复印 --- 原型模式]({{< relref "/docs/chapter9" >}})
33+
34+
- [第十章 考题抄错会做也白搭 --- 模版方法模式]({{< relref "/docs/chapter10" >}})
35+
36+
- [第十一章 无熟人难办事 --- 迪米特法则]({{< relref "/docs/chapter11" >}})
37+
38+
- [第十二章 牛市股票还会亏钱 --- 外观模式]({{< relref "/docs/chapter12" >}})
39+
40+
- [第十三章 好菜每回味不同 --- 建造者模式]({{< relref "/docs/chapter13" >}})
41+
42+
- [第十四章 老板回来,我不知道 --- 观察者模式]({{< relref "/docs/chapter14" >}})
43+
44+
- [第十五章 就不能不换DB吗? --- 抽象工厂模式]({{< relref "/docs/chapter5" >}})
45+
46+
- [第十六章 无尽加班何时休 --- 状态模式]({{< relref "/docs/chapter16" >}})
47+
48+
- [第十七章 在NBA我需要翻译 --- 适配器模式]({{< relref "/docs/chapter17" >}})
49+
50+
- [第十八章 如果再回到从前 --- 备忘录模式]({{< relref "/docs/chapter18" >}})< 10000 /div>
51+
52+
- [第十九章 分公司 = 一部分 --- 组合模式]({{< relref "/docs/chapter19" >}})
53+
54+
- [第二十章 想走?可以!先买票 --- 迭代器模式]({{< relref "/docs/chapter20" >}})
55+
56+
- [第二十一章 有些类也需要计划生育 --- 单例模式]({{< relref "/docs/chapter21" >}})
57+
58+
- [第二十二章 手机软件何时统一 --- 桥接模式]({{< relref "/docs/chapter22" >}})
59+
60+
- [第二十三章 烤羊肉串引来的思考 --- 命令模式]({{< relref "/docs/chapter23" >}})
61+
62+
- [第二十四章 加薪非要老总批 --- 职责链模式]({{< relref "/docs/chapter24" >}})
63+
64+
- [第二十五章 世界需要和平 --- 中介者模式]({{< relref "/docs/chapter25" >}})
65+
66+
- [第二十六章 项目多也别傻做 --- 享元模式]({{< relref "/docs/chapter26" >}})
67+
68+
- [第二十七章 其实你不懂老板的心 --- 解释器模式]({{< relref "/docs/chapter27" >}})
69+
70+
- [第二十八章 男人和女人 --- 访问者模式]({{< relref "/docs/chapter28" >}})
71+
72+
- [第二十九章 OOTV杯超级模式大赛 --- 模式总结]({{< relref "/docs/chapter29" >}})
73+
74+
75+
## License
76+
77+
The MIT License.

book/content/docs/chapter1.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: 第一章 代码无错就是优 --- 简单工厂模式
3+
type: docs
4+
weight: 2
5+
---
6+
7+
### 第一章 代码无错就是优 --- 简单工厂模式
8+
9+
1.简单工厂模式
10+
```php
11+
<?php
12+
13+
/**
14+
* Operation
15+
*/
16+
class Operation
17+
{
18+
protected $a = 0;
19+
protected $b = 0;
20+
21+
public function setA($a)
22+
{
23+
$this->a = $a;
24+
}
25+
26+
public function setB($b)
27+
{
28+
$this->b = $b;
29+
}
30+
31+
public function getResult()
32+
{
33+
$result = 0;
34+
return $result;
35+
}
36+
}
37+
38+
/**
39+
* Add
40+
*/
41+
class OperationAdd extends Operation
42+
{
43+
public function getResult()
44+
{
45+
return $this->a + $this->b;
46+
}
47+
}
48+
49+
/**
50+
* Mul
51+
*/
52+
class OperationMul extends Operation
53+
{
54+
public function getResult()
55+
{
56+
return $this->a * $this->b;
57+
}
58+
}
59+
60+
/**
61+
* Sub
62+
*/
63+
class OperationSub extends Operation
64+
{
65+
public function getResult()
66+
{
67+
return $this->a - $this->b;
68+
}
69+
}
70+
71+
/**
72+
* Div
73+
*/
74+
class OperationDiv extends Operation
75+
{
76+
public function getResult()
77+
{
78+
return $this->a / $this->b;
79+
}
80+
}
81+
82+
/**
83+
* Operation Factory
84+
*/
85+
class OperationFactory
86+
{
87+
public static function createOperation($operation)
88+
{
89+
switch ($operation) {
90+
case '+':
91+
$oper = new OperationAdd();
92+
break;
93+
case '-':
94+
$oper = new OperationSub();
95+
break;
96+
case '/':
97+
$oper = new OperationDiv();
98+
break;
99+
case '*':
100+
$oper = new OperationMul();
101+
break;
102+
}
103+
104+
return $oper;
105+
}
106+
}
107+
108+
// 客户端代码
109+
$operation = OperationFactory::createOperation('+');
110+
$operation->setA(1);
111+
$operation->setB(2);
112+
echo $operation->getResult() . PHP_EOL;
113+
```
114+
115+
116+
117+
总结
118+
119+
> 学会通过分封装,继承,多态把程序的藕合度降低
120+
121+
> 复用,不是复制!
122+
123+
> 高内聚,低耦合
124+
125+
下一章:[第二章 商场促销 --- 策略模式]({{< relref "/docs/chapter2" >}})

book/content/docs/chapter10.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
title: 第十章 考题抄错会做也白搭 --- 模版方法模式
3+
type: docs
4+
weight: 11
5+
---
6+
7+
### 第十章 考题抄错会做也白搭 --- 模版方法模式
8+
```php
9+
<?php
10+
// 对甲乙两名同学所抄试卷,尽量将相同的部分提到父类
11+
// 金庸小说考题试卷
12+
class TestPaper
13+
{
14+
public function TestQuestion1()
15+
{
16+
echo "杨过说过,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[]a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维 \n";
17+
echo "答案 ".$this->answer1()."\n";
18+
}
19+
20+
public function TestQuestion2()
21+
{
22+
echo "杨过、程英、陆无双铲除了情花,造成[]a.使这种植物不在害人 b.使一种珍惜物种灭绝 c.破坏了那个生态圈的生态平衡 d.造成该地区沙漠化 \n";
23+
echo "答案 ".$this->answer2()."\n";
24+
}
25+
26+
public function TestQuestion3()
27+
{
28+
echo "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[]a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对 \n";
29+
echo "答案 ".$this->answer3()."\n";
30+
}
31+
32+
protected function answer1()
33+
{
34+
return '';
35+
}
36+
37+
protected function answer2()
38+
{
39+
return '';
40+
}
41+
42+
protected function answer3()
43+
{
44+
return '';
45+
}
46+
}
47+
48+
// 学生甲抄的试卷
49+
class TestPaperA extends TestPaper
50+
{
51+
protected function answer1()
52+
{
53+
return 'a';
54+
}
55+
56+
protected function answer2()
57+
{
58+
return 'b';
59+
}
60+
61+
protected function answer3()
62+
{
63+
return 'c';
64+
}
65+
}
66+
67+
// 学生乙抄的试卷
68+
// 学生甲抄的试卷
69+
class TestPaperB extends TestPaper
70+
{
71+
protected function answer1()
72+
{
73+
return 'd';
74+
}
75+
76+
protected function answer2()
77+
{
78+
return 'c';
79+
}
80+
81+
protected function answer3()
82+
{
83+
return 'a';
84+
}
85+
}
86+
87+
88+
// 客户端代码
89+
90+
echo "学生甲抄的试卷: \n";
91+
$student = new TestPaperA();
92+
$student->TestQuestion1();
93+
$student->TestQuestion2();
94+
$student->TestQuestion3();
95+
echo "学生乙抄的试卷: \n";
96+
$student2 = new TestPaperB();
97+
$student2->TestQuestion1();
98+
$student2->TestQuestion2();
99+
$student2->TestQuestion3();
100+
```
101+
102+
总结:
103+
104+
> ***模版方法模式***,定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模版方法使得子类可以不改变算法的节结构即可重定义该算法的某些特定步骤。
105+
106+
> 既然用了继承,并且肯定这个继承有意义,就应该要成为子类的模版,所有重复的代码都应该要上升到父类去,而不是让每个子类去重复。
107+
108+
> 当我们要完成在某一细节层次一致的一个过程或一系列步骤,但其中个别步骤在更详细的层次上的实现可能不同时,我们通常考虑用模版方法模式来处理。
109+
110+
> 模版方法模式是通过把不变行为搬移到超类,去除子类中的重复代码来体现它的优势。提供了一个很好的代码复用平台。
111+
112+
> 当不变的和可变的行为在方法的子类实现中混合在一起的时候,不变的行为就会在子类中重复出现。通过模版方法把这些行为搬移到单一的地方,这样就帮助子类摆脱重复的不变行为的纠缠。
113+
114+
上一章:[第九章 简历复印 --- 原型模式]({{< relref "/docs/chapter9" >}})
115+
116+
下一章:[第十一章 无熟人难办事 --- 迪米特法则]({{< relref "/docs/chapter11" >}})

book/content/docs/chapter11.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: 第十一章 无熟人难办事 --- 迪米特法则
3+
type: docs
4+
weight: 12
5+
---
6+
7+
### 第十一章 无熟人难办事 --- 迪米特法则
8+
9+
总结:
10+
11+
> ***迪米特法则***,如果两个类不彼此直接通信,那么这两个类就不应当发生直接的相互作用。如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用。
12+
13+
> 在类的结构设计上,每一个类都应当 尽量降低成员的访问权限
14+
15+
> 类之间的耦合越弱,越有利于复用,一个处在弱耦合的类被修改,不会对有关系的类造成波及。
16+
17+
18+
上一章:[第十章 考题抄错会做也白搭 --- 模版方法模式]({{< relref "/docs/chapter10" >}})
19+
20+
下一章:[第十二章 牛市股票还会亏钱 --- 外观模式]({{< relref "/docs/chapter12" >}})

0 commit comments

Comments
 (0)
0