解釋器模式-Interpreter 目前沒有想到其他更好的例子, 以後本文肯定重寫,或者儘力介紹得更到位....目前只是copy了iluwatar大神的代碼, 作為練習, 也方便自己以後查找本段代碼. Expression介面 表達式的統一定義. PlusExpression類 加法表達式. Min ...
解釋器模式-Interpreter
目前沒有想到其他更好的例子, 以後本文肯定重寫,或者儘力介紹得更到位....目前只是copy了iluwatar大神的代碼, 作為練習, 也方便自己以後查找本段代碼.
Expression介面
表達式的統一定義.
public interface Expression { int interpret(); @Override String toString(); }
PlusExpression類
加法表達式.
/** * 加號 */ public class PlusExpression implements Expression { private Expression leftExpression; private Expression rightExpression; public PlusExpression(Expression leftExpression, Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; } @Override public int interpret() { return leftExpression.interpret() + rightExpression.interpret(); } @Override public String toString() { return "+"; } }
MinusExpression類
減法表達式.
/** * 減號 */ public class MinusExpression implements Expression { private Expression leftExpression; private Expression rightExpression; public MinusExpression(Expression leftExpression, Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; } @Override public int interpret() { return leftExpression.interpret() - rightExpression.interpret(); } @Override public String toString() { return "-"; } }
MultiplyExpression類
乘法表達式.
/** * 乘號 */ public class MultiplyExpression implements Expression { private Expression leftExpression; private Expression rightExpression; public MultiplyExpression(Expression leftExpression, Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; } @Override public int interpret() { return leftExpression.interpret() * rightExpression.interpret(); } @Override public String toString() { return "*"; } }
NumberExpression類
數字也是一種表達式. 或者叫做語句中的一類元素
/** * 數字 */ public class NumberExpression implements Expression { private int number; public NumberExpression(int number) { this.number = number; } public NumberExpression(String s) { this.number = Integer.parseInt(s); } @Override public int interpret() { return number; } @Override public String toString() { return "number"; } }
App類
運行/場景模擬
import java.util.Stack; public class App { public static void main(String[] args) { String tokenString = "4 3 2 - 1 + *"; Stack<Expression> stack = new Stack<>(); String[] tokenList = tokenString.split(" "); for (String s : tokenList) { if (isOperator(s)) { Expression rightExpression = stack.pop(); Expression leftExpression = stack.pop(); System.out.printf("popped from stack left: %s right: %s\n", leftExpression.interpret(), rightExpression.interpret()); Expression operator = getOperatorInstance(s, leftExpression, rightExpression); System.out.printf("operator: %s\n", operator); int result = operator.interpret(); NumberExpression resultExpression = new NumberExpression(result); stack.push(resultExpression); System.out.printf("push result to stack: %s\n", resultExpression.interpret()); } else { Expression i = new NumberExpression(s); stack.push(i); System.out.printf("push to stack: %s\n", i.interpret()); } } System.out.printf("result: %s\n", stack.pop().interpret()); } private static boolean isOperator(String s) { return s.equals("+") || s.equals("-") || s.equals("*"); } private static Expression getOperatorInstance(String s, Expression left, Expression right) { switch (s) { case "+": return new PlusExpression(left, right); case "-": return new MinusExpression(left, right); case "*": return new MultiplyExpression(left, right); default: return new MultiplyExpression(left, right); } } }
運行結果如下: