zno2

16) Interpreter pattern

类别:

 behavioral pattern

问题:

Polish notation https://en.wikipedia.org/wiki/Polish_notation

Abstract syntax tree https://en.wikipedia.org/wiki/Abstract_syntax_tree

BNF https://en.wikipedia.org/wiki/Interpreter_pattern

recursion https://en.wikipedia.org/wiki/Recursion 

 

 

方案:

 

import java.util.ArrayList;
import java.util.List;

public class InterpreterPatternDemo {
    public static void main(String[] args) {
        Parser context = new Parser();
        ExpressionBase parse = context.parse("+ - 6 5 7");
        System.out.println(parse.evaluate());
    }
}

abstract class ExpressionBase {
    public abstract int evaluate();
}

/** 数字 */
class IntegerExpression extends ExpressionBase {
    private int value;

    public IntegerExpression(int value) {
        this.value = value;
    }

    @Override
    public int evaluate() {
        return value;
    }

}

/** 加法 */
class AdditionExpression extends ExpressionBase {
    private ExpressionBase expr1;
    private ExpressionBase expr2;

    public AdditionExpression(ExpressionBase expr1, ExpressionBase expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    @Override
    public int evaluate() {
        int value1 = expr1.evaluate();
        int value2 = expr2.evaluate();
        return value1 + value2;
    }

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

/** 减法 */
class SubtractionExpression extends ExpressionBase {
    private ExpressionBase expr1;
    private ExpressionBase expr2;

    public SubtractionExpression(ExpressionBase expr1, ExpressionBase expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    @Override
    public int evaluate() {
        int value1 = expr1.evaluate();
        int value2 = expr2.evaluate();
        return value1 - value2;
    }

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

/***/
class Parser {
    
    public ExpressionBase parse(String polish) {
        List<String> symbolList = new ArrayList<String>();
        String[] split = polish.split(" ");
        for (String string : split) {
            symbolList.add(string);
        }

        return parseNextExpression(symbolList);
    }

    private ExpressionBase parseNextExpression(List<String> symbolList) {
        try {

            int value = Integer.parseInt(symbolList.get(0));
            symbolList.remove(0);
            return new IntegerExpression(value);
        } catch (NumberFormatException e) {
            return ParseNonTerminalExpression(symbolList);
        }
    }

    private ExpressionBase ParseNonTerminalExpression(List<String> symbolList) {
        String symbol = symbolList.get(0);
        symbolList.remove(0);

        ExpressionBase expr1 = parseNextExpression(symbolList);
        ExpressionBase expr2 = parseNextExpression(symbolList);

        switch (symbol) {
        case "+":
            return new AdditionExpression(expr1, expr2);
        case "-":
            return new SubtractionExpression(expr1, expr2);
        default:
            String message = "Invalid Symbol (" + symbol + ")";
            throw new IllegalArgumentException(message);
        }
    }
}

 

 8

 

示例:

 

应用:

 

不足:(

 

优化:)

 

posted on 2023-06-09 13:36  zno2  阅读(7)  评论(0编辑  收藏  举报

导航