Java single application - Architecture Mode - 03. Design mode - 16. Interpreter mode

Posted by tlavelle on Fri, 13 Mar 2020 11:25:34 +0100

Original address: http://www.work100.net/training/monolithic-architecture-design-patterns-interpreter-pattern.html
More tutorials: Beam cloud - free course

Interpreter mode

Serial number Chapter in text video
1 Summary -
2 Realization -

Please refer to the navigation above for reading

1. overview

Interpreter Pattern provides a way to evaluate the syntax or expression of a language. It belongs to behavioral pattern. This pattern implements an expression interface that interprets a specific context. This mode is used in SQL parsing, symbol processing engine, etc.

Intention:

Given a language, define its grammatical representation, and define an interpreter that uses that token to interpret sentences in the language.

Main solutions:

For some fixed grammars, build an interpreter to interpret sentences.

When to use:

If a particular type of problem occurs frequently enough, it may be worth expressing each instance of the problem as a sentence in a simple language. This allows you to build an interpreter that solves the problem by interpreting these sentences.

How to solve:

Build a syntax tree to define terminators and non terminators.

Key code:

Build environment class, including some global information outside the interpreter, generally HashMap.

Application example:

Compiler, operation expression evaluation.

Advantage:

  • Good scalability and flexibility.
  • New ways to interpret expressions have been added.
  • Easy to implement simple grammar.

Disadvantages:

  • There are few available scenarios.
  • It is difficult to maintain complex grammar.
  • Interpreter mode causes class inflation.
  • The interpreter mode uses the recursive call method.

Usage scenario:

  • Sentences in a language that needs to be interpreted and executed can be represented as an abstract syntax tree.
  • Some recurring problems can be expressed in a simple language.
  • A simple grammar needs to explain the scene.

matters needing attention:

There are few available scenarios. If you encounter it in Java, you can use expression4J instead.

2. implementation

We will create an interface Expression and an entity class that implements the Expression interface.

Defines the TerminalExpression class as the primary interpreter in the context. Other classes OrExpression and AndExpression are used to create composite expressions.

Interpretpatterndemo, our demo class uses the Expression class to create rules and demo Expression parsing.

Step 1

Create an expression interface.

Expression.java, the code is as follows:

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

Step 2

Create an entity class that implements the above interface.

TerminalExpression.java, the code is as follows:

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, the code is as follows:

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, the code is as follows:

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

Interpretpatterndemo uses the Expression class to create rules and parse them.

Interpretpatterndemo.java, the code is as follows:

public class InterpreterPatternDemo {

   //Rule: Robert and John are men
   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"));
   }
}

Step 4

Execute the program and output the result:

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

Last article: Command mode
Next article: Iterator mode

If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.


Topics: Java SQL