解释器模式主要用于编译器,解释一门语言,下面我们来演示一下四则运算,逐步将解释器模式应用到运算中,先来看一段没有使用设计模式的四则运算的代码(递归处理子表达式):
public class Interpreter_01 {
private static final List<Character> symbols = new ArrayList<>(Arrays.asList('+','-','*','/','%','(',')'));
public static void main(String[] args) {
String expression = readExpression();
Map<Character, Integer> expressionRelValue = readValue(expression);
int result = run(expression, expressionRelValue);
System.out.println("运算结果("+expression+")="+result);
}
private static String readExpression() {
System.out.print("请输入表达式:");
Scanner scanner = new Scanner(System.in);
return scanner.next();
}
private static Map<Character, Integer> readValue(String expression){
Map<Character, Integer> expressionRelValue = new HashMap<>();
if(null!=expression && !"".equals(expression)){
char[] chars = expression.toCharArray();
for (int i = 0; i < chars.length; i++) {
char var = chars[i];
if(!symbols.contains(var)){
System.out.print(var+"=");
Scanner scanner = new Scanner(System.in);
expressionRelValue.put(var,Integer.valueOf(scanner.next()));
}
}
}
return expressionRelValue;
}
private static int run(String expression, Map<Character, Integer> expressionRelValue){
char[] chars = expression.toCharArray();
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < chars.length; i++) {
char var = chars[i];
switch (var){
case '+':
return stack.push(stack.pop()+run(expression.substring(i+1), expressionRelValue));
case '-':
return stack.push(stack.pop()-run(expression.substring(i+1), expressionRelValue));
case '(':
return stack.push(run(expression.substring(i+1), expressionRelValue));
case ')':
break;
default:
stack.push(expressionRelValue.get(var));
break;
}
}
return stack.pop();
}
}
运行结果:
请输入表达式:a+b+c-d
a=2
b=3
c=4
d=1
运算结果(a+b+c-d)=8
以上是最简单的设计,没有依赖任何的类,就完成了四则运算。这样的设计只能解决一时问题,如果希望能够支持后续进行优雅的扩展,那么需要对整个运算解析逻辑重新设计,
首先分析下计算逻辑:
- 如果是运算符号,那么必定两边都会有数值或者是表达式,否则表达式错误
- 如果是前括号"(",那么需要循环处理内部的子表达式,直到遇到后括号")",子表达式运算结束
- 每次运算结束的值都需要压入栈,在下次计算时在出栈
重新设计后代码如下:
public class Interpreter_02 {
private static final List<Character> symbols = new ArrayList<>(Arrays.asList('+','-','*','/','%','(',')'));
public static void main(String[] args) {
String expression = readExpression();
Map<Character, Integer> expressionRelValue = readValue(expression);
AbstractExpression abstractExpression = run(expression, expressionRelValue);
System.out.println("运算结果("+expression+")="+abstractExpression.interpreter(expressionRelValue));
}
private static String readExpression() {
System.out.print("请输入表达式:");
Scanner scanner = new Scanner(System.in);
return scanner.next();
}
private static Map<Character, Integer> readValue(String expression){
Map<Character, Integer> expressionRelValue = new HashMap<>();
if(null!=expression && !"".equals(expression)){
char[] chars = expression.toCharArray();
for (int i = 0; i < chars.length; i++) {
char var = chars[i];
if(!symbols.contains(var)){
System.out.print(var+"=");
Scanner scanner = new Scanner(System.in);
expressionRelValue.put(var,Integer.valueOf(scanner.next()));
}
}
}
return expressionRelValue;
}
private static AbstractExpression run(String expression, Map<Character, Integer> expressionRelValue){
char[] chars = expression.toCharArray();
Stack<AbstractExpression> stack = new Stack<>();
for (int i = 0; i < chars.length; i++) {
char var = chars[i];
switch (var){
case '+':
return stack.push(new AddExpression(stack.pop(),run(expression.substring(i+1), expressionRelValue)));
case '-':
return stack.push(new SubExpression(stack.pop(),run(expression.substring(i+1), expressionRelValue)));
case '(':
return stack.push(new LeftParenthesesExpression(run(expression.substring(i+1), expressionRelValue)));
case ')':
break;
default:
stack.push(new VarExpression(var));
break;
}
}
return stack.pop();
}
abstract static class AbstractExpression{
public abstract int interpreter(Map<Character, Integer> map);
}
static class VarExpression extends AbstractExpression{
private char key;
public VarExpression(char key) {
this.key = key;
}
@Override
public int interpreter(Map<Character, Integer> map) {
return map.get(this.key);
}
}
abstract static class SymbolExpression extends AbstractExpression{
private AbstractExpression left;
private AbstractExpression right;
public SymbolExpression(AbstractExpression left, AbstractExpression right) {
this.left = left;
this.right = right;
}
}
static class AddExpression extends SymbolExpression{
public AddExpression(AbstractExpression left, AbstractExpression right) {
super(left, right);
}
@Override
public int interpreter(Map<Character, Integer> map) {
return super.left.interpreter(map)+super.right.interpreter(map);
}
}
static class SubExpression extends SymbolExpression{
public SubExpression(AbstractExpression left, AbstractExpression right) {
super(left, right);
}
@Override
public int interpreter(Map<Character, Integer> map) {
return super.left.interpreter(map)-super.right.interpreter(map);
}
}
static class LeftParenthesesExpression extends SymbolExpression{
public LeftParenthesesExpression(AbstractExpression right) {
super(null, right);
}
@Override
public int interpreter(Map<Character, Integer> map) {
return super.right.interpreter(map);
}
}
}
重构后的代码,主要将运算分成了两部分,一部分是key对应的数值,一部分是运算符号。但是这两部分最终都要返回一个相同的结果,所以继承了同一个抽象类,并实现各自的处理逻辑。这就完成了一个简单的加减法运算的解析。
解释器模式的官方定义:给定一门语言,定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。
优点:
扩展性好
缺点:
类膨胀
调试复杂
如果循环次数过大和递归深度越深,效率越低