Interpreter Pattern
Posted nedrain
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Interpreter Pattern相关的知识,希望对你有一定的参考价值。
Question
Theory
An Example
// Expression.java
import java.util.HashMap;
public abstract class Expression {
public abstract int interpreter(HashMap<String , Integer> var);
}
// VarExpression.java
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);
}
}
// SymbolExpression.java
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;
}
}
// AddExpression.java
import java.util.HashMap;
public class AddExpression extends SymbolExpression {
public AddExpression(Expression left, Expression right){
super(left, right);
}
@Override
public int interpreter(HashMap<String, Integer> var) {
return super.left.interpreter(var) + super.right.interpreter(var);
}
}
// SubExpression.java
import java.util.HashMap;
public class SubExpression extends SymbolExpression {
public SubExpression(Expression left, Expression right){
super(left, right);
}
@Override
public int interpreter(HashMap<String, Integer> var) {
return super.left.interpreter(var) + super.right.interpreter(var);
}
}
// Calculator.java
import java.util.HashMap;
import java.util.Stack;
public class Calculator {
private Expression expression;
public Calculator(String expStr){
Stack<Expression> stack = new Stack<Expression>();
char[] charArray = expStr.toCharArray();
Expression left = null;
Expression right = null;
for (int i = 0; i < charArray.length; i++) {
switch (charArray[i]){
case ‘+‘:
left = stack.pop();
right = new VarExpression(String.valueOf(charArray[++i]));
stack.push(new AddExpression(left, right));
break;
case ‘-‘:
left = stack.pop();
right = new VarExpression(String.valueOf(charArray[++i]));
stack.push(new SubExpression(left, right));
break;
default:
stack.push(new VarExpression(String.valueOf(charArray[i])));
}
}
this.expression = stack.pop();
}
public int run(HashMap<String, Integer> var){
return this.expression.interpreter(var);
}
}
// Client.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Client {
public static void main(String[] args) {
String expStr = getExpStr();
HashMap<String, Integer> var = getValue(expStr);
Calculator calculator = new Calculator(expStr);
System.out.println("Result: " + expStr + " = "+ calculator.run(var));
}
public static String getExpStr(){
System.out.println("Please Input Expressions : ");
try {
return (new BufferedReader(new InputStreamReader(System.in))).readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static HashMap<String, Integer> getValue(String expStr){
HashMap<String, Integer> map = new HashMap<String, Integer>();
for(char ch: expStr.toCharArray()){
if(ch != ‘+‘ && ch != ‘-‘){
if(!map.containsKey(String.valueOf(ch))){
System.out.println("Please input the value of : " + String.valueOf(ch));
try {
String in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
map.put(String.valueOf(ch), Integer.valueOf(in));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return map;
}
}
以上是关于Interpreter Pattern的主要内容,如果未能解决你的问题,请参考以下文章
二十四种设计模式:解释器模式(Interpreter Pattern)
尚硅谷设计模式学习(21)---[解释器模式(Interpreter Pattern)]
-bash: /usr/bin/ls: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory(代码片段