一.定义与类型
1.定义:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子
为了解释一种语言,而为语言创建的解释器
2.类型:行为型
二.适应类型
1.在某个特定类型问题发生频率足够高
三.优点
语法由很多类表示,容易改变及扩展此“语言”
四.缺点
当语法规则数目太多时,增加了系统复杂度
五.UML类图
六.代码
Interpreter.java
package com.caomingyu.autotest.interpreter;
public interface Interpreter {
int interpret();
}
AddInterpreter.java
package com.caomingyu.autotest.interpreter;
public class AddInterpreter implements Interpreter {
private Interpreter firstExpression,secondExpression;
public AddInterpreter(Interpreter firstExpression, Interpreter secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}
public int interpret() {
return firstExpression.interpret() + secondExpression.interpret();
}
@Override
public String toString() {
return "+";
}
}
MultiInterpreter.java
package com.caomingyu.autotest.interpreter;
public class MultiInterpreter implements Interpreter {
private Interpreter firstExpression,secondExpression;
public MultiInterpreter(Interpreter firstExpression, Interpreter secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}
public int interpret() {
return firstExpression.interpret() * secondExpression.interpret();
}
@Override
public String toString() {
return "*";
}
}
NumberInterpreter.java
package com.caomingyu.autotest.interpreter;
public class NumberInterpreter implements Interpreter {
private int number;
public NumberInterpreter(int number) {
this.number = number;
}
public NumberInterpreter(String number) {
this.number = Integer.parseInt(number);
}
public int interpret() {
return this.number;
}
}
CExpressionParser.java
package com.caomingyu.autotest.interpreter;
import java.util.Stack;
public class CExpressionParser {
private Stack<Interpreter> stack = new Stack<Interpreter>();
public int parse(String str){
String[] strItemArray = str.split(" ");
for (String symbol: strItemArray){
if (!OperatorUtil.isOperator(symbol)){
Interpreter numberExpression = new NumberInterpreter(symbol);
stack.push(numberExpression);
System.out.println(String.format("入栈:%d",numberExpression.interpret()));
} else {
//是可运算符号可以计算
Interpreter firstExpression = stack.pop();
Interpreter secondExpression = stack.pop();
System.out.println(String.format("出栈:%d 和 %d",firstExpression.interpret(),secondExpression.interpret()));
Interpreter operator = OperatorUtil.getExpressionObject(firstExpression, secondExpression, symbol);
System.out.println(String.format("应用运算符:%s",operator));
int result = operator.interpret();
NumberInterpreter resultExpression = new NumberInterpreter(result);
stack.push(resultExpression);
System.out.println(String.format("阶段结果入栈:%d",resultExpression.interpret()));
}
}
int result = stack.pop().interpret();
return result;
}
}
OperatorUtil.java
package com.caomingyu.autotest.interpreter;
public class OperatorUtil {
public static boolean isOperator(String symbol) {
return symbol.equals("+") || symbol.equals("*");
}
public static Interpreter getExpressionObject(Interpreter firstExpression, Interpreter secondExpression, String symbol) {
if ("+".equals(symbol)){
return new AddInterpreter(firstExpression, secondExpression);
} else if ("*".equals(symbol)){
return new MultiInterpreter(firstExpression, secondExpression);
}
return null;
}
}
Test.java
package com.caomingyu.autotest.interpreter;
public class Test {
public static void main(String[] args) {
String cInputStr = "6 100 11 + *";
CExpressionParser expressionParser = new CExpressionParser();
int result = expressionParser.parse(cInputStr);
System.out.println("解释器计算结果:"+result);
}
}
该设计模式用的比较少,我们平时很少写解释器