Technology-设计模式-解释器模式

本文介绍了GoF中的解释器模式。

模式推演

用简单的语言控制鸭子

Technology-DesignPattern-Interpreter-Request

归纳语法

Technology-DesignPattern-Interpreter-Language

将每种语法用一个类表示

Technology-DesignPattern-Interpreter-Class

代码实例

Expression接口

1
2
3
public interface Expression {
public boolean interpret(String context);
}

Variable类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Variable implements Expression {
private static int currentHour = 13;
private String name;
public Variable(String name){
this.name = name;
}
@Override
public boolean interpret(String context) {
if(this.name.equalsIgnoreCase("daylight")){
System.out.println("Hour:" + currentHour);
if(currentHour >= 6 && currentHour <= 18){
++currentHour;
currentHour %= 24;
return true;
}
}
++currentHour;
currentHour %= 24;
return false;
}
}

Repetition & Sequence

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Repetition implements Expression {
private Variable var = null;
private Expression expr = null;
public Repetition(Variable var, Expression expr){
this.var = var;
this.expr = expr;
}
@Override
public boolean interpret(String context) {
while(this.var.interpret(context)){
this.expr.interpret(context);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return false;
}
}
class Sequence implements Expression {
private Expression expr1 = null;
private Expression expr2 = null;
public Sequence(Expression expr1, Expression expr2){
this.expr1 = expr1;
this.expr2 = expr2;
}
@Override
public boolean interpret(String context) {
this.expr1.interpret(context);
if(this.expr2 != null)
this.expr2.interpret(context);
return false;
}
}

QuackCommand & RightCommand & FlyCommand

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class QuackCommand implements Expression {
@Override
public boolean interpret(String context) {
System.out.println("Duck Quack..");
return false;
}
}
class RightCommand implements Expression {
@Override
public boolean interpret(String context) {
System.out.println("Duck Right..");
return false;
}
}
class FlyCommand implements Expression {
@Override
public boolean interpret(String context) {
System.out.println("Duck Fly..");
return false;
}
}

语法解析Parse类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Parser {
public static void parse(String context){
parseExpression(context).interpret(null);
}
private static Variable parseVariable(String context){
byte chars[] = context.getBytes();
for(int i = 0; i < chars.length; ++i){
byte c = chars[i];
if(!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z')){
return null;
}
}
return new Variable(context);
}
private static Expression parseExpression(String context){
Expression expression = parseCommand(context);
if(expression == null){
expression = parseSequence(context);
if(expression == null){
expression = parseRepetition(context);
}
}
return expression;
}
private static Expression parseSequence(String context){
int split = context.indexOf(';');
if(split != -1)
return new Sequence(parseExpression(context.substring(0, split)), parseExpression(context.substring(split + 1)));
return null;
}
private static Expression parseRepetition(String context) {
String left = "while(";
if(context.startsWith(left)){
int right = context.indexOf(')');
if(right > 6){
return new Repetition(parseVariable(context.substring(left.length(), right)), parseExpression(context.substring(right + 1)));
}
}
return null;
}
private static Expression parseCommand(String context){
if(context.equalsIgnoreCase("right"))
return new RightCommand();
if(context.equalsIgnoreCase("quack"))
return new QuackCommand();
if(context.equalsIgnoreCase("fly"))
return new FlyCommand();
return null;
}
}

Main函数

1
2
3
4
5
6
7
public class Main {
public static void main(String[] args) {
Parser.parse("right;while(daylight)fly;quack;");
}
}

执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Duck Right..
Hour:13
Duck Fly..
Hour:14
Duck Fly..
Hour:15
Duck Fly..
Hour:16
Duck Fly..
Hour:17
Duck Fly..
Hour:18
Duck Fly..
Hour:19
Duck Quack..

定义

解释器模式(Interpreter Pattern):为语言创建解释器。

用途:

  • 用于实现一门简单的语言;
  • 当有一个简单的语法,而且简单比效率更重要时,使用解释器;
  • 可以处理脚本语言和编程语言。

优点:

  • 将每一个语法规则表示成一个类,方便于实现语言;
  • 因为语法由很多类表示,所以可以轻易地改变或扩展此语言;
  • 通过在类结构中加入新的方法,可以在解释的同时增加新的行为,例如打印格式的美化或者进行复杂的长须验证。

缺点:

  • 当语法规则的数目太大时,这个模式可能会变得非常繁杂。在这种情况下,使用解析器/编译器的产生器可能更合适。
YI wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!