1 代码演练
1.1 代码演练1(解释器模式coding)
1.2 代码演练如何应用了解释器模式
1 代码演练
1.1 代码演练1(解释器模式coding)(该案例运用了栈的先进先出的特性)
需求:
原系统中有大量的处理计算的类:处理的规则有(a+b)*c*d+e和a/b+c-d等等数量众多方法;如果设计成coding中的类之后,直接一个类就可以搞定。提高了类的复用性,简化了代码。
uml类图:
测试类:
package com.geely.design.pattern.behavioral.interpreter; /**
* Created by geely.
*/
public class Test {
public static void main(String[] args) {
String geelyInputStr="6 100 11 + *";
GeelyExpressionParser expressionParser=new GeelyExpressionParser();
int result=expressionParser.parse(geelyInputStr);
System.out.println("解释器计算结果: "+result);
}
}
解释器实现类:
package com.geely.design.pattern.behavioral.interpreter; import java.util.Stack; /**
* Created by geely.
*/
public class GeelyExpressionParser {
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);
System.out.println(String.format("入栈: %d", numberExpression.interpret()));
} else {
//是运算符号,可以计算
Interpreter firstExpression = stack.pop();
Interpreter secondExpression = stack.pop();
System.out.println(String.format("出栈: %d 和 %d",
firstExpression.interpret(), secondExpression.interpret()));
Interpreter operator = OperatorUtil.getExpressionObject(firstExpression, secondExpression, symbol);
System.out.println(String.format("应用运算符: %s", operator));
int result = operator.interpret();
NumberInterpreter resultExpression = new NumberInterpreter(result);
stack.push(resultExpression);
System.out.println(String.format("阶段结果入栈: %d", resultExpression.interpret()));
}
}
int result = stack.pop().interpret();
return result; }
}
计算工具类:
package com.geely.design.pattern.behavioral.interpreter; /**
* Created by geely.
*/
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;
}
}
解释器接口:
package com.geely.design.pattern.behavioral.interpreter; /**
* Created by geely.
*/
public interface Interpreter {
int interpret();
}
解释器实现类1(数字解释器实现类):
package com.geely.design.pattern.behavioral.interpreter; /**
* Created by geely.
*/
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;
}
}
解释器实现类2(乘法解释器实现类):
package com.geely.design.pattern.behavioral.interpreter; /**
* Created by geely.
*/
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 "*";
} }
解释器实现类3(加法解释器实现类):
package com.geely.design.pattern.behavioral.interpreter; /**
* Created by geely.
*/
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(){
return this.firstExpression.interpret()+this.secondExpression.interpret();
}
@Override
public String toString(){
return "+";
}
}
打印结果:
入栈: 6
入栈: 100
入栈: 11
出栈: 11 和 100
应用运算符: +
阶段结果入栈: 111
出栈: 111 和 6
应用运算符: *
阶段结果入栈: 666
解释器计算结果: 666 Process finished with exit code 0
1.2 代码演练如何应用了解释器模式
加法解释器里面是使用的“+”,乘法解释器里面是使用“*”,通过解释器模式将表达式的结果正确输出.最核心的还是parse()方法,它的功能就是解析表达式。
加法,乘法,各种解释器,他们都是利用了一个parse就解决了。也就是把解析的工作和具体业务相解耦,就可以有更大的扩展性。代码的复用只是好处之一。