移动架构之解释器模式

解释器是一种用的比较少的行为模式,其提供了一种解释语言的语法,或者表达式的方式。该模式定义了一个表达式的接口

应用场景

  1. 简单的语言需要解释执行而且可以将该语言中的语句表示一个抽象的语法树
  2. 对于某个特定的领域出现的不断重复的问题,可以转换成一种语法规则下的语句

重点

  1. 必须有一个抽象接口
  2. 构建语法树

优缺点

优点:
每个语法都要产生一个非终结符表达式,语法规则比较复杂时,就可能产生大量的类文件,为维护带来了非常多的麻烦
缺点:
解释器模式由于使用了大量的循环和递归,效率是个不容忽视的问题,特别是用于解析复杂、冗长的语法时,效率是难以忍受的

解释实例

这里使用一个解析计算表达式的例子
抽象接口

1
2
3
public interface Node {
public int interpret();
}

抽象接口的子类抽象类

1
2
3
4
5
6
7
8
9
10
public abstract class SymbolNode implements Node{

protected Node left;
protected Node right;

public SymbolNode(Node left, Node right) {
this.left = left;
this.right = right;
}
}

实现类

1
2
3
4
5
6
7
8
9
10
11
public class MulNode extends SymbolNode{

public MulNode(Node left, Node right) {
super(left, right);
}

@Override
public int interpret() {
return left.interpret() * right.interpret();
}
}

1
2
3
4
5
6
7
8
9
10
11
public class DivNode extends SymbolNode{

public DivNode(Node left, Node right) {
super(left, right);
}

@Override
public int interpret() {
return left.interpret() / right.interpret();
}
}

终结符

1
2
3
4
5
6
7
8
9
10
11
12
13
public class ValueNode implements Node{

private int value;

public ValueNode(int value) {
this.value = value;
}

@Override
public int interpret() {
return value;
}
}

计算方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Caculator {
public int build(String statement) {
Node left = null;
Node right = null;
Node last = null;
String[] state = statement.split(" ");
for (int i = 0; i < state.length; i++) {
if("*".equalsIgnoreCase(state[i])){
left = last;
int val = Integer.parseInt(state[++i]);
right = new ValueNode(val);
last = new MulNode(left, right);
}else if ("/".equalsIgnoreCase(state[i])) {
left = last;
int val = Integer.parseInt(state[++i]);
right = new ValueNode(val);
last = new DivNode(left, right);
}else {
last = new ValueNode(Integer.parseInt(state[i]));
}
}
return last.interpret();
}
}

测试类

1
2
3
4
5
6
7
8
public class Test {
public static void main(String[] args) {
String contentString = "1 * 2 * 5 * 7 * 9 / 2";
Caculator caculator = new Caculator();
int result = caculator.build(contentString);
System.out.println("result = " + result);
}
}

Donate comment here