解释器模式-Interpreter(Java实现)

时间:2021-10-31 17:11:18

解释器模式-Interpreter

目前没有想到其他更好的例子, 以后本文肯定重写,或者尽力介绍得更到位....目前只是copy了iluwatar大神的代码, 作为练习, 也方便自己以后查找本段代码.

Expression接口

表达式的统一定义.

public interface Expression {

    int interpret();

    @Override
    String toString();
}

PlusExpression类

加法表达式.

/**
 * 加号
 */
public class PlusExpression implements Expression {

    private Expression leftExpression;
    private Expression rightExpression;

    public PlusExpression(Expression leftExpression, Expression rightExpression) {
        this.leftExpression = leftExpression;
        this.rightExpression = rightExpression;
    }

    @Override
    public int interpret() {
        return leftExpression.interpret() + rightExpression.interpret();
    }

    @Override
    public String toString() {
        return "+";
    }
}

MinusExpression类

减法表达式.

/**
 * 减号
 */
public class MinusExpression implements Expression {

    private Expression leftExpression;
    private Expression rightExpression;

    public MinusExpression(Expression leftExpression, Expression rightExpression) {
        this.leftExpression = leftExpression;
        this.rightExpression = rightExpression;
    }

    @Override
    public int interpret() {
        return leftExpression.interpret() - rightExpression.interpret();
    }

    @Override
    public String toString() {
        return "-";
    }

}

MultiplyExpression类

乘法表达式.

/**
 * 乘号
 */
public class MultiplyExpression implements Expression {

    private Expression leftExpression;
    private Expression rightExpression;

    public MultiplyExpression(Expression leftExpression, Expression rightExpression) {
        this.leftExpression = leftExpression;
        this.rightExpression = rightExpression;
    }

    @Override
    public int interpret() {
        return leftExpression.interpret() * rightExpression.interpret();
    }

    @Override
    public String toString() {
        return "*";
    }

}

NumberExpression类

数字也是一种表达式. 或者叫做语句中的一类元素

/**
 * 数字
 */
public class NumberExpression implements Expression {

    private int number;

    public NumberExpression(int number) {
        this.number = number;
    }

    public NumberExpression(String s) {
        this.number = Integer.parseInt(s);
    }

    @Override
    public int interpret() {
        return number;
    }

    @Override
    public String toString() {
        return "number";
    }
}

App类

运行/场景模拟

import java.util.Stack;

public class App {

    public static void main(String[] args) {
        String tokenString = "4 3 2 - 1 + *";
        Stack<Expression> stack = new Stack<>();

        String[] tokenList = tokenString.split(" ");
        for (String s : tokenList) {
            if (isOperator(s)) {
                Expression rightExpression = stack.pop();
                Expression leftExpression = stack.pop();
                System.out.printf("popped from stack left: %s right: %s\n",
                        leftExpression.interpret(), rightExpression.interpret());
                Expression operator = getOperatorInstance(s, leftExpression, rightExpression);
                System.out.printf("operator: %s\n", operator);
                int result = operator.interpret();
                NumberExpression resultExpression = new NumberExpression(result);
                stack.push(resultExpression);
                System.out.printf("push result to stack: %s\n", resultExpression.interpret());
            } else {
                Expression i = new NumberExpression(s);
                stack.push(i);
                System.out.printf("push to stack: %s\n", i.interpret());
            }
        }
        System.out.printf("result: %s\n", stack.pop().interpret());
    }

    private static boolean isOperator(String s) {
        return s.equals("+") || s.equals("-") || s.equals("*");
    }

    private static Expression getOperatorInstance(String s, Expression left, Expression right) {
        switch (s) {
            case "+":
                return new PlusExpression(left, right);
            case "-":
                return new MinusExpression(left, right);
            case "*":
                return new MultiplyExpression(left, right);
            default:
                return new MultiplyExpression(left, right);
        }
    }
}

运行结果如下:

解释器模式-Interpreter(Java实现)