Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Interpreter mode


May 27, 2021 Design mode


Table of contents


Interpreter mode

Interpreter Pattern provides a way to evaluate the syntax or expression of a language, which is a behavioral pattern. T his pattern implements an expression interface that interprets a particular context. This pattern is used in SQL resolution, symbol processing engines, and so on.

Introduced

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

Main solution: Build an interpreter that interprets sentences for some fixed laws.

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

How to solve: Component syntax tree that defines terminators and non-terminators.

Key code: A component environment class that contains some global information other than an interpreter, typically HashMap.

Application example: compiler, op-expression evaluation.

Pros: 1, scalability is better, flexible. 2 , added a new way to interpret expressions. 3, easy to implement simple law.

Cons: 1, there are fewer available scenes. 2 , for complex law is more difficult to maintain. 3 , interpreter mode will cause class expansion. 4, interpreter mode using recursive call method.

Use scenario: 1, you can represent a sentence in a language that needs to be interpreted for execution as an abstract syntax tree. 2 , some recurring problems can be expressed in a simple language. 3, a simple syntax needs to explain the scene.

Note: There are fewer scenarios available, and if encountered in JAVA you can use expression4J instead.

Realize

We'll create an interface Expression and an entity class that implements the Expression interface. D efine the TerminalExpression class as the primary interpreter in the context. Other classes OrExpression and AndExpression are used to create combined expressions.

Interpreter PotternDemo, our demo class uses the Expression class to create rules and demonstrate expression resolution.

Interpreter mode

Step 1

Create an expression interface.

Expression.java

public interface Expression {
   public boolean interpret(String context);
}

Step 2

Create an entity class that implements the above interface.

TerminalExpression.java

public class TerminalExpression implements Expression {
    
   private String data;

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

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

OrExpression.java

public class OrExpression implements Expression {
     
   private Expression expr1 = null;
   private Expression expr2 = null;

   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);
   }
}

AndExpression.java

public class AndExpression implements Expression {
     
   private Expression expr1 = null;
   private Expression expr2 = null;

   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);
   }
}

Step 3

InterpreterPatternDemo uses Expression classes to create rules and parse them.

InterpreterPatternDemo.java

public class InterpreterPatternDemo {

   //规则:Robert 和 John 是男性
   public static Expression getMaleExpression(){
      Expression robert = new TerminalExpression("Robert");
      Expression john = new TerminalExpression("John");
      return new OrExpression(robert, john);     
   }

   //规则:Julie 是一个已婚的女性
   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"));
   }
}

Step 4

Verify the output.

John is male? true
Julie is a married women? true