解释器模式
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.interpreter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加法解释器
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/5 14:46
|
||||||
|
*/
|
||||||
|
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() {
|
||||||
|
// 此时传入为NumberInterpreter类型,调interpret方法转int类型
|
||||||
|
return this.firstExpression.interpret() + this.secondExpression.interpret();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "+";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.interpreter;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/5 14:48
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class CustomExpressionParser {
|
||||||
|
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);
|
||||||
|
log.info(String.format("入栈: %d", numberExpression.interpret()));
|
||||||
|
} else {
|
||||||
|
//是运算符号,可以计算
|
||||||
|
Interpreter firstExpression = stack.pop();
|
||||||
|
Interpreter secondExpression = stack.pop();
|
||||||
|
log.info(String.format("出栈: %d 和 %d",
|
||||||
|
firstExpression.interpret(), secondExpression.interpret()));
|
||||||
|
Interpreter operator = OperatorUtil.getExpressionObject(firstExpression, secondExpression, symbol);
|
||||||
|
log.info(String.format("应用运算符: %s", operator));
|
||||||
|
int result = operator.interpret();
|
||||||
|
NumberInterpreter resultExpression = new NumberInterpreter(result);
|
||||||
|
stack.push(resultExpression);
|
||||||
|
log.info(String.format("阶段结果入栈: %d", resultExpression.interpret()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int result = stack.pop().interpret();
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.interpreter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解释器
|
||||||
|
* <p>
|
||||||
|
* JDK 源码
|
||||||
|
* {@link java.util.regex.Pattern} 正则解释器
|
||||||
|
* {@link org.springframework.expression.spel.standard.SpelExpressionParser} Spring解释器
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/5 14:45
|
||||||
|
*/
|
||||||
|
public interface Interpreter {
|
||||||
|
int interpret();
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.interpreter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 乘法解释器
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/5 14:47
|
||||||
|
*/
|
||||||
|
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 "*";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.interpreter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/5 14:47
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.interpreter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/5 14:47
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.interpreter;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.expression.Expression;
|
||||||
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/5 14:48
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
class InterpreterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void interpret() {
|
||||||
|
String myInterpreter = "6 100 11 + *";
|
||||||
|
CustomExpressionParser expressionParser = new CustomExpressionParser();
|
||||||
|
int result = expressionParser.parse(myInterpreter);
|
||||||
|
log.info("解释器计算结果: " + result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void springTest() {
|
||||||
|
org.springframework.expression.ExpressionParser parser = new SpelExpressionParser();
|
||||||
|
Expression expression = parser.parseExpression("100 * 2 + 400 * 1 + 66");
|
||||||
|
int result = (Integer) expression.getValue();
|
||||||
|
log.info(String.valueOf(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user