Interpreter mode of design pattern 21

background

In software development, some similar functions may appear multiple times, and these functions have certain similarities and regularities. This is how we can summarize it into a simple language. This is the source of the interpreter pattern.

Is the above description in the cloud and mist? Let's give an example. We want to develop a function that requires input of formulas, input parameters, and returns correct results. For example, we enter the parameters:

a=2;b=4;c=10

We then enter the expression:

a+b-c

Output result: -4.

Let's enter another expression:

a * b / c

Output result: 0.8.

If you were to develop this feature, what would you do? If you use the interpreter mode, there will be good results.

What is interpreter mode

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. Use this representation to interpret sentences in the language.)

The interpreter mode mainly consists of the following five elements:

  • Abstract Expression (Abstract Expression) role: defines the interface of the interpreter, agrees on the interpretation operation of the interpreter, and mainly includes the interpretation method interpret().

  • The role of Terminal Expression: It is a subclass of abstract expressions used to implement operations related to terminals in the grammar. Each terminal in the grammar has a specific terminal expression corresponding to it.

  • The role of Nonterminal Expression: It is also a subclass of abstract expressions, used to implement operations related to nonterminal symbols in the grammar. Each rule in the grammar corresponds to a nonterminal expression.

  • Context role: usually contains data or common functions required by each interpreter, and is generally used to transfer data shared by all interpreters, and subsequent interpreters can obtain these values ​​from here.

  • Client: The main task is to convert the sentence or expression that needs to be analyzed into an abstract syntax tree described by the interpreter object, and then call the interpretation method of the interpreter. Of course, the interpretation method of the interpreter can also be accessed indirectly through the environment role. .

The structure diagram is as follows:

Interpreter mode

Code

Let's take a look at how the code is implemented:

Context

public class Context {
    private AbstractExpression exp;

    public Context() {
        //数据初始化
    }

    public void operation(String info) {
        //调用相关表达式类的解释方法
    }
}

AbstractExpression

public interface AbstractExpression {
    Object interpret(String info);    //解释方法
}

TerminalExpression

public class TerminalExpression implements AbstractExpression{
    @Override
    public Object interpret(String info) {
        System.out.println("进入终结符表达式的处理逻辑");
        return null;
    }
}

NonterminalExpression

public class NonterminalExpression implements AbstractExpression {
    private AbstractExpression exp1;
    private AbstractExpression exp2;
    @Override
    public Object interpret(String info) {
        System.out.println("进入非终结表达式的逻辑");
        return null;
    }
}

Because the interpreter mode is a relatively seldom used mode. The above template has been given, you can think about how to use this mode.

Thinking about interpreter mode

The interpreter mode often has efficiency issues and will increase the complexity of maintenance. Like the question at the beginning of the article, we can use the mathematical parser formula Expression4J, MESP (Math Expression String Parser) and Jep in Java. Can explain some complex expressions. It is simpler to use than the original interpreter mode.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

  1. 回复"java" 获取java电子书;

  2. 回复"python"获取python电子书;

  3. 回复"算法"获取算法电子书;

  4. 回复"大数据"获取大数据电子书;

  5. 回复"spring"获取SpringBoot的学习视频。

  6. 回复"面试"获取一线大厂面试资料

  7. 回复"进阶之路"获取Java进阶之路的思维导图

  8. 回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

  9. 回复"总结"获取Java后端面试经验总结PDF版

  10. 回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

  11. 回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/110038130