[Behavioral Mode] Interpreter Mode

Reference in this article rookie tutorial and ThinkWon The singleton pattern explained:

Interpreter Pattern (Interpreter Pattern) provides a way to evaluate the grammar or expressions of the language, it is a behavioral pattern. This pattern implements an expression interface that interprets a specific context. This mode is used in SQL parsing, symbol processing engines, etc.

introduce

Intent: Given a language, define its grammatical representation and define an interpreter that uses this identity to interpret sentences in the language.

Main solution: build an interpreter that interprets sentences for some fixed grammars.

When to use: If a particular type of problem occurs frequently enough, it may be worth expressing the instances of the problem as sentences in a simple language. This makes it possible to build an interpreter that solves the problem by interpreting these sentences.

How to solve it: Build a syntax tree, define terminal symbols and non-terminal symbols.

Key code: build environment class, which contains some global information outside the interpreter, usually HashMap.

Application examples: Compiler, arithmetic expression calculation.

Advantages: 1. Good scalability and flexibility. 2. Added a new way of interpreting expressions. 3, easy to implement simple grammar.

Disadvantages: 1. There are few available scenarios. 2. It is difficult to maintain complex grammars. 3. The interpreter pattern can cause class bloat. 4. The interpreter mode adopts the recursive calling method.

Usage scenarios: 1. A sentence in a language that needs to be interpreted and executed can be represented as an abstract syntax tree. 2. Some recurring problems can be expressed in a simple language. 3. A scenario where a simple grammar needs to be explained.

Matters needing attention: There are few available scenarios. If you encounter it in JAVA, you can use expression4J instead.

accomplish

We will create an interface Expression and an entity class that implements the Expression interface. Defines the TerminalExpression class that is the primary interpreter in the context. The other classes OrExpression, AndExpression are used to create composite expressions.

InterpreterPatternDemo, our demo class uses the Expression class to create rules and demonstrate parsing of expressions.

Example:
Expression interface:

package expression;

public interface Expression {

    public boolean interpret(String context);

}

TerminalExpression class:

package expression;

public class TerminalExpression implements Expression {

    String data;

    public TerminalExpression(String data) {
        this.data = data;
    }

    @Override
    public boolean interpret(String context) {
        if (context.contains(data)) {
            return true;
        }
        return false;
    }
}

AndExpression class:

package expression;

public class AndExpression implements Expression {

    Expression expr1;
    Expression expr2;

    public AndExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    @Override
    public boolean interpret(String context) {
        return expr1.interpret(context) && expr2.interpret(context);
    }
}

OrExpression class:

package expression;

public class OrExpression implements Expression {

    Expression expr1;
    Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    @Override
    public boolean interpret(String context) {
        return expr1.interpret(context) || expr2.interpret(context);
    }
}

InterpreterPatternDemo class:

import expression.AndExpression;
import expression.Expression;
import expression.OrExpression;
import expression.TerminalExpression;

public class InterpreterPatternDemo {

    //Rule: Robert and John are male
    public static Expression getMaleExpression(){
        Expression robert = new TerminalExpression("Robert");
        Expression john = new TerminalExpression("John");
        return new OrExpression(robert, john);
    }

    //Rule: Julie is a married woman
    public static Expression getMarriedWomanExpression(){
        Expression julie = new TerminalExpression("Julie");
        Expression married = new TerminalExpression("Married");
        return new AndExpression(julie, married);
    }

    public static void main(String[] args) {
        Expression isMale = getMaleExpression();
        Expression isMarriedWoman = getMarriedWomanExpression();

        System.out.println("John is male? " + isMale.interpret("John"));
        System.out.println("Julie is a married women? "
                + isMarriedWoman.interpret("Married Julie"));
    }

}

Tags: Design Pattern

Posted by NJordan72 on Tue, 31 May 2022 08:19:09 +0530