码迷,mamicode.com
首页 > 其他好文 > 详细

Interpreter Pattern

时间:2020-06-29 18:29:08      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:new   input   ade   NPU   break   lin   case   ack   xtend   

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

标签:new   input   ade   NPU   break   lin   case   ack   xtend   

原文地址:https://www.cnblogs.com/nedrain/p/13209377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!