一個解釋器模式中包含的四種角色html
使用解釋器模式實現一個簡單的語法 計算 6 100 11 + * 表達式,首先記錄數值,而後按照順序添加符號計算。
100 + 11
111 * 6
666
/** * 表達式定義接口 */ 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<>(); public int parse (String expression) { String[] itemArray = expression.split(" "); for (String symbol : itemArray) { 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.getExpression(firstExpression, secondExpression, symbol); System.out.println(String.format("應用運算符: %s", operator)); int result = operator.interpret(); NumberInterpreter numberInterpreter = new NumberInterpreter(result); stack.push(numberInterpreter); System.out.println(String.format("階段結果入棧: %d", result)); } } return stack.pop().interpret(); } } /** * 工具類 */ public class OperatorUtil { public static boolean isOperator(String symbol) { return (symbol.equals("+") || symbol.equals("*")); } public static Interpreter getExpression(Interpreter first, Interpreter second, String symbol) { switch (symbol) { case "+": return new AddInterpreter(first, second); case "*": return new MultiInterpreter(first, second); default: return null; } } }
/** * 測試類 */ public class Test { public static void main(String[] args) { String input = "6 100 11 + *"; ExpressionParser parser = new ExpressionParser(); int result = parser.parse(input); System.out.println("解釋器運算結果:" + result); } }
入棧: 6 入棧: 100 入棧: 11 出棧:11 和 100 應用運算符: + 階段結果入棧: 111 出棧:111 和 6 應用運算符: * 階段結果入棧: 666 解釋器運算結果:666
在樣例中:AddInterpreter和MultiInterpreter爲終結表達式,NumberInterpreter爲非終結表達式,ExpressionParser爲環境角色。java
解釋器模式和適配器模式git
慕課網設計模式精講
: https://coding.imooc.com/class/270.html 23種設計模式(14):解釋器模式
: https://blog.csdn.net/zhengzhb/article/details/7666020