解释器模式
- 定义:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子
- 简单来说:为了解释一种语言而为语言创建的解释器
- 类型:行为型
- 适用场景:某个特定类型问题发生频率足够高
- 优点:语法由很多类表示,容易改变及扩展此“语言”
- 缺点:当语法规则数目太多时,增加了系统复杂度
- 相关设计模式:
- 解释器模式和适配器模式:有点类似,但是适配器模式不需要预先知道要适配的规则,而解释器要把规则写好,根据规则去执行解释
Coding
- 场景:实现6 100 11 + *按顺序数字入栈,遇到符号出栈计算结果
/** * 解释器接口以及它的三个实现类 */ public interface Interpreter { int interpret(); } public class AddInterpreter implements Interpreter { private Interpreter firstExpression,secondExpression; public AddInterpreter(Interpreter firstExpression, Interpreter secondExpression) { this.firstExpression = firstExpression; this.secondExpression = secondExpression; } @Override public int interpret() { return this.firstExpression.interpret() + this.secondExpression.interpret(); } @Override public String toString() { return "+"; } } public class MultiInterpreter implements Interpreter { private Interpreter firstExpression, secondExpression; public MultiInterpreter(Interpreter firstExpression, Interpreter secondExpression) { this.firstExpression = firstExpression; this.secondExpression = secondExpression; } @Override public int interpret() { return this.firstExpression.interpret() * this.secondExpression.interpret(); } @Override public String toString() { return "*"; } } public class NumberInterpreter implements Interpreter { private int number; public NumberInterpreter(int number) { this.number = number; } public NumberInterpreter(String number) { this.number = Integer.parseInt(number); } @Override public int interpret() { return this.number; } } public class ExpressionParser { 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; } } 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 (symbol.equals("+")) { return new AddInterpreter(firstExpression, secondExpression); } else if (symbol.equals("*")) { return new MultiInterpreter(firstExpression, secondExpression); } return null; } }
测试
public class Test { public static void main(String[] args) { String inputStr = "6 100 11 + *"; ExpressionParser expressionParser = new ExpressionParser(); int result = expressionParser.parse(inputStr); System.out.println("解释器计算结果: " + result); } } ============输出================ 入栈: 6 入栈: 100 入栈: 11 出栈: 11 和 100 应用运算符: + 阶段结果入栈: 111 出栈: 111 和 6 应用运算符: * 阶段结果入栈: 666 解释器计算结果: 666
UML
源码中的应用
java.util.regex.Pattern:正则表达式就是一种语法,通过JDK中的正则解释器把它解释出来
public final class Pattern implements java.io.Serializable { private int parsePastWhitespace(int ch) { while (ASCII.isSpace(ch) || ch == '#') { while (ASCII.isSpace(ch)) ch = temp[cursor++]; if (ch == '#') ch = parsePastLine(); } return ch; } /** * xmode parse past comment to end of line. */ private int parsePastLine() { int ch = temp[cursor++]; while (ch != 0 && !isLineSeparator(ch)) ch = temp[cursor++]; return ch; }