Interpreter mode
Recommended distributed micro service e-commerce source code
Interpreter Pattern provides a way to evaluate the syntax or expression of a language. It is a behavioral pattern. This pattern implements an expression interface that interprets a specific context. This pattern is used in SQL parsing, symbol processing engine and so on.
introduce
Intention: given a language, define its grammatical representation, and define an interpreter that uses the identifier to interpret sentences in the language.
The main solution: for some fixed grammars, build an interpreter to explain sentences.
When to use: if the frequency of a particular type of problem is high enough, it may be worth expressing each instance of the problem as a sentence in a simple language. In this way, you can build an interpreter that solves the problem by interpreting these sentences.
How to solve this problem: build a syntax tree and define terminators and non terminators.
Key code: build environment class, which contains some global information outside the interpreter, generally HashMap.
Application examples: compiler, operation expression calculation.
Advantages: 1. Good scalability and flexibility. 2. Added a new way to interpret expressions. 3. Easy to implement simple grammar.
Disadvantages: 1. There are few available scenarios. 2. It is difficult to maintain complex grammar. 3. Interpreter mode causes class inflation. 4. The Interpreter pattern uses recursive call methods.
Usage scenario: 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 simple syntax needs to be explained.
Note: there are few available scenarios. If encountered in JAVA, expression4J can be used instead.
realization
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.
InterpreterPatternDemo, our demo class uses the Expression class to create rules and demonstrate the parsing of expressions.
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 the Expression class to create rules and parse them.
InterpreterPatternDemo.java
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 results:
John is male? true Julie is a married women? true