introduce
Role:
It mainly includes the following roles:
- Expression: abstract expression.
- TerminalExpression: Terminator expression - is a symbolic expression.
- NonTerminalExpression: non terminator expression - numeric expression.
- Calculator: calculation class.
Code
Code scenario
Interpreter to add and subtract.
Specific code
Expression
/** * Abstract interpreter * @author GaoYuan * @date 2018/11/11 8:38 a.m. */ public abstract class Expression { /** Explanation action */ public abstract int interpret(); }
ValueExpression - interpret numbers
/** * Digital interpreter (specific value) TerminalExpression * @author GaoYuan * @date 2018/11/11 8:44 a.m. */ public class ValueExpression extends Expression { private int value; public ValueExpression(){} public ValueExpression(int value){ this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } @Override public int interpret() { return this.value; } }
SymbolExpression - explain operator (Abstract)
/** * Operator interpreter NonterminalExpression * @author GaoYuan * @date 2018/11/11 8:43 a.m. */ public abstract class SymbolExpression extends Expression { /** Left result */ protected Expression left; /** Right result */ protected Expression right; public SymbolExpression(Expression left, Expression right) { this.left=left; this.right=right; } }
Explanation class of concrete operator
/** * addition * @author GaoYuan * @date 2018/11/11 1:43 p.m. */ public class AddExpression extends SymbolExpression { /** It must be a value type, not a symbol */ public AddExpression(Expression left, Expression right){ super(left, right); } @Override public int interpret() { // Left is interpreted as the value of left, right as the value of right return left.interpret() + right.interpret(); } } /** * subtraction * @author GaoYuan * @date 2018/11/11 1:43 p.m. */ public class SubExpression extends SymbolExpression { /** It must be a value type, not a symbol */ public SubExpression(Expression left, Expression right){ super(left, right); } @Override public int interpret() { // Left is interpreted as the value of left, right as the value of right return left.interpret() - right.interpret(); } }
Calculator - calculation class. Calculation is required after interpretation
/** * Calculation * @author GaoYuan * @date 2018/11/11 1:50 p.m. */ public class Calculator { /** * Calculation * @author GaoYuan * @date 2018/11/11 2:01 p.m. */ public int calculator(String statement){ Expression left; Expression right; // Used to store the currently calculated value Expression now = null; // The data separated by spaces is intercepted into an array String[] strs = statement.split(" "); for(int i=0;i<strs.length;i++){ if("+".equals(strs[i])){ // The object on the left is the currently calculated value left = now; // ++i, that is, it is calculated with the latter number, because it is currently an operator right = new ValueExpression(Integer.parseInt(strs[++i])); // Execution addition now = new AddExpression(left, right); }else if("-".equals(strs[i])){ // The object on the left is the currently calculated value left = now; // Calculate with the number on the right right = new ValueExpression(Integer.parseInt(strs[++i])); // Subtraction now = new SubExpression(left, right); }else{ // Numbers, generally speaking, the first and last must be numbers now = new ValueExpression(Integer.parseInt(strs[i])); } } // Explain that all - now is the encapsulated final expression tree return now.interpret(); } }
Test class
public static void main(String[] args){ String statement = "1 + 2 + 3 - 5"; Calculator calculator = new Calculator(); int result = calculator.calculator(statement); System.out.println(statement + " = " + result); }
output
1 + 2 + 3 - 5 = 1
Advantages and disadvantages
Advantage:
- The symbol interpretation class can be extended, for example, operators such as multiplication and division can be added
- It can be used for SQL interpretation, etc.
Disadvantages:
- Because the process needs to be explained, it is time-consuming.
- Expressions that are too long are not easy to maintain.
- There are few scenes.
Code cloud
Blog
https://my.oschina.net/gmarshal/blog/2964024
Welcome to my personal wechat subscription number: (it is said that this head portrait app is dedicated to apes)