//20200202
写在前面:刚期末考试完,考了面向对象,里边儿有23个设计模式,我寻思着考完挨个儿实现一下,本文实现解释器模式
解释器模式核心思想
- 构建语法树,然后将输入的值根据语法树执行
程序源代码:
- 此处实现一个加减运算的解释器,输入算式,输入参数,输出结果
import java.util.HashMap;
/**
* 每个运算符只需要管其旁边的两个参数即可
*/
public abstract class Expression {
//解析公式和数值,key是公式中的参数,value是具体的数值
public abstract int interpreter(HashMap<String,Integer> var);
}
import java.util.HashMap;
import java.util.Stack;
public class Caculator {
//定义表达式
private Expression expression;
//构造函数传参,并解析
public Caculator(String expstr){
//安排运算先后顺序
Stack<Expression> stack = new Stack<>();
//表达式拆分为字符数组
char[] cArray = expstr.toCharArray();
Expression left = null;
Expression right = null;
for(int i = 0;i<cArray.length;++i){
switch (cArray[i]){
case '+':
//加法
left = stack.pop();
right = new VarExpression(String.valueOf(cArray[++i]));
stack.push(new AddExpression(left,right));
break;
case '-':
//减法
left = stack.pop();
right = new VarExpression((String.valueOf(cArray[++i])));
stack.push(new SubExpression(left,right));
break;
default:
//运算式中变量
stack.push(new VarExpression(String.valueOf(cArray[i])));
break;
}
}
this.expression = stack.pop();
}
//计算
public int run(HashMap<String,Integer> var){
return this.expression.interpreter(var);
}
}
import java.util.HashMap;
/**
* 加法解释
*/
public class AddExpression extends SymbolExpression{
public AddExpression(Expression left,Expression right){
super(left,right);
}
public int interpreter(HashMap<String, Integer> var) {
return super.left.interpreter(var)+super.right.interpreter(var);
}
}
import java.util.HashMap;
/**
* 减法解析
*/
public class SubExpression extends SymbolExpression{
public SubExpression(Expression left,Expression right){
super(left,right);
}
public int interpreter(HashMap<String, Integer> var) {
return super.left.interpreter(var)-super.right.interpreter(var);
}
}
import java.util.HashMap;
/**
* 符号解析
*/
public class SymbolExpression extends Expression{
protected Expression left;
protected Expression right;
public SymbolExpression(Expression left,Expression right){
this.left = left;
this.right = right;
}
@Override
public int interpreter(HashMap<String, Integer> var) {
return 0;
}
}
import java.util.HashMap;
/**
* 参数解析
*/
public class VarExpression extends Expression{
private String key;
public VarExpression(String key){
this.key = key;
}
@Override
public int interpreter(HashMap<String, Integer> var) {
return var.get(this.key);
}
}
import java.util.HashMap;
import java.util.Scanner;
/**
* 测试主类
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入参数表达式(仅限加减运算):");
String expStr = sc.next();
Caculator caculator = new Caculator(expStr);
HashMap<String,Integer> map = new HashMap<>();
for(char ch : expStr.toCharArray()) {
if(ch != '+' && ch != '-' ) {
if(! map.containsKey(String.valueOf(ch))) {
System.out.print("请输入" + String.valueOf(ch) + "的值:");
String in = sc.next();
map.put(String.valueOf(ch), Integer.valueOf(in));
}
}
}
System.out.println(caculator.run(map));
}
}
- 输出如下:
至此,23个设计模式全部结束/撒花/✿✿ヽ(°▽°)ノ✿
以上
希望对大家有所帮助