解释器模式(Interpreter Pattern)

2024/3/31 Java进阶

定义一个语法,并通过自定义解释器进行解释翻译,得到想要的结果。

# 1、解释器案例

A+B+C-D通过自定义一套语法实现加减法。

public interface Expression {
    int interptet();
}
1
2
3

加法解释器

public class AddExpression implements Expression {
    private Expression num1;
    private Expression num2;

    public AddExpression(Expression num1, Expression num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    @Override
    public int interptet() {
        return num1.interptet() + num2.interptet();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

减法解释器

public class MinusExpression implements Expression {
    private Expression num1;
    private Expression num2;

    public MinusExpression(Expression num1, Expression num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    @Override
    public int interptet() {
        return num1.interptet() - num2.interptet();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

数据容器

public class NumberData implements Expression {
    private int sum;

    public NumberData(int sum) {
        this.sum = sum;
    }

    @Override
    public int interptet() {
        return sum;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
public class ClientTest {
    public static void main(String[] args) {
        Expression num1 = new NumberData(1);
        Expression num2 = new NumberData(2);
        Expression num3 = new NumberData(3);
        Expression num4 = new NumberData(1);

        // 1+2
        AddExpression addExpression = new AddExpression(num1, num2);
        // (1+2)+3
        AddExpression addExpression2 = new AddExpression(addExpression, num3);
        // (1+2)+3-1
        MinusExpression minusExpression = new MinusExpression(addExpression2, num4);

        System.out.println(minusExpression.interptet());
    }
}
// 输出结果:5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 2、解释器数据封装

Expression、AddExpression、MinusExpression保持不变

封装数据方法

public class NumberData implements Expression {
    private int sum;
    private Queue<Expression> expressionQueue = new LinkedList<>();

    private NumberData(int sum) {
        this.sum = sum;
    }

    public NumberData(String expression) {
        // 第一位是否为负数
        Boolean firstIsMinus = expression.startsWith("-");
        StringTokenizer minusToken = new StringTokenizer(expression,"-");
        while (minusToken.hasMoreTokens()){
            String minusNum = minusToken.nextToken();
            StringTokenizer addToken = new StringTokenizer(minusNum,"+");
            if(addToken.countTokens()>1||!firstIsMinus){
                firstIsMinus = true;
                while (addToken.hasMoreTokens()){
                    String addNum = addToken.nextToken();
                    // 加法解释器
                    expressionQueue.offer(new AddExpression(this,new NumberData(Integer.parseInt(addNum))));
                }
            }else {
                // 减法解释器
                expressionQueue.offer(new MinusExpression(this,new NumberData(Integer.parseInt(minusNum))));
            }
        }
    }

    @Override
    public int interptet() {
        while (!expressionQueue.isEmpty()){
            Expression poll = expressionQueue.poll();
            sum = poll.interptet();
        }
        return sum;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class ClientTest {
    public static void main(String[] args) {
        Expression num1 = new NumberData("1+2+3+4-5");
        System.out.println(num1.interptet());
    }
}
// 输出结果:5
1
2
3
4
5
6
7