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

Skip to content

Commit 35bd347

Browse files
committed
complete chapter 28
1 parent 31ca034 commit 35bd347

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

codes/Interpreter.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
interface AbstractExpression
4+
{
5+
public function interpret(Context $context);
6+
}
7+
8+
class TerminalExpression implements AbstractExpression
9+
{
10+
public function interpret(Context $context)
11+
{
12+
echo "终端解释器\n";
13+
}
14+
}
15+
16+
class NonTerminalExpression implements AbstractExpression
17+
{
18+
public function interpret(Context $context)
19+
{
20+
echo "非终端解释器\n";
21+
}
22+
}
23+
24+
class Context
25+
{
26+
private $input;
27+
public function setInput($input)
28+
{
29+
$this->input = $input;
30+
}
31+
32+
public function getInput()
33+
{
34+
return $this->input;
35+
}
36+
37+
private $output;
38+
public function setOutput($output)
39+
{
40+
$this->output = $output;
41+
}
42+
43+
public function getOutput()
44+
{
45+
return $this->output;
46+
}
47+
}
48+
49+
50+
$context = new Context();
51+
$syntax = [];
52+
array_push($syntax, new TerminalExpression());
53+
array_push($syntax, new NonTerminalExpression());
54+
array_push($syntax, new TerminalExpression());
55+
array_push($syntax, new TerminalExpression());
56+
57+
foreach ($syntax as $value) {
58+
$value->interpret($context);
59+
}
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+

files/chapter27.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,77 @@
11
### 第二十七章 其实你不懂老板的心 --- 解释器模式
22

3+
```php
4+
<?php
5+
6+
interface AbstractExpression
7+
{
8+
public function interpret(Context $context);
9+
}
10+
11+
class TerminalExpression implements AbstractExpression
12+
{
13+
public function interpret(Context $context)
14+
{
15+
echo "终端解释器\n";
16+
}
17+
}
18+
19+
class NonTerminalExpression implements AbstractExpression
20+
{
21+
public function interpret(Context $context)
22+
{
23+
echo "非终端解释器\n";
24+
}
25+
}
26+
27+
class Context
28+
{
29+
private $input;
30+
public function setInput($input)
31+
{
32+
$this->input = $input;
33+
}
34+
35+
public function getInput()
36+
{
37+
return $this->input;
38+
}
39+
40+
private $output;
41+
public function setOutput($output)
42+
{
43+
$this->output = $output;
44+
}
45+
46+
public function getOutput()
47+
{
48+
return $this->output;
49+
}
50+
}
51+
52+
53+
$context = new Context();
54+
$syntax = [];
55+
array_push($syntax, new TerminalExpression());
56+
array_push($syntax, new NonTerminalExpression());
57+
array_push($syntax, new TerminalExpression());
58+
array_push($syntax, new TerminalExpression());
59+
60+
foreach ($syntax as $value) {
61+
$value->interpret($context);
62+
}
63+
```
64+
65+
> ***解释器模式***,给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言的句子。
66+
67+
> 如果一种特定语言类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表述为一个简单语言的句子。这样就可以构建一个解释器,该解释器通过解释这些句子来解决该问题。
68+
69+
> 通常当有一个语言需要解释执行,并且你可将该语言的句子表示为一个抽象语法树时,可使用解释器模式。
70+
71+
> 解释器模式容易修改和扩展文法,因为解释器模式使用类来表示文法规则,你可使用继承来改变或扩展该文法。也比较容易实现文法,因为定义抽象语法树中各个节点的类的实现大体类似,这些类都易于直接编写。
72+
73+
> 解释器模式不足的是要为文法中的每一条规则至少定义了一个类,因此包含许多规则的文法可能难以管理和维护。建议当文法非常复杂时,使用其他的技术如语法分析程序或编译器生成器来处理。
74+
375
上一章:[第二十六章 项目多也别傻做 --- 享元模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/files/chapter26.md)
476

5-
下一章:[第二十八章](https://github.com/flyingalex/design-patterns-by-php/blob/master/files/chapter28.md)
77+
下一章:[第二十八章 男人和女人 --- 访问者模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/files/chapter28.md)

files/chapter28.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
### 第二十八章 男人和女人 --- 访问者模式
2+
3+
总结
4+
5+
> ***访问者模式***
6+
7+
8+
上一章:[第二十七章 其实你不懂老板的心 --- 解释器模式](https://github.com/flyingalex/design-patterns-by-php/blob/master/files/chapter27.md)
9+
10+
下一章:[第二十九章 ](https://github.com/flyingalex/design-patterns-by-php/blob/master/files/chapter29.md)

0 commit comments

Comments
 (0)
0