preface

The advantage of the interpreter pattern, which is used infrequently, is that many complex sentences can be parsed using grammar rules that can be simple or easy, such as regular expressions to match numeric strings.

directory

A, definitions,

A syntax parsing scheme, less commonly used in real projects, defined as a representation of a given language that defines its grammar and defines an interpreter that uses that representation to interpret sentences in the language.

Two, mode principle analysis

Public interface Expression {Boolean interpreter(String con); } //2. Public class TerminalExpression implements Expression{String data implements Expression; public TerminalExpression(String data) { this.data = data; } @Override public boolean interpreter(String con) { if(con.contains(data)) { return true; } else { return false; }}} public class AndExpression implements Expression {Expression implements expr1; Expression expr2; public AndExpression(Expression expr1, Expression expr2) { this.expr1 = expr1; this.expr2 = expr2; } public boolean interpreter(String con) { return expr1.interpreter(con) && expr2.interpreter(con); }} public class name implements Expression {list implements expr1; Expression expr2; public OrExpression(Expression expr1, Expression expr2) { this.expr1 = expr1; this.expr2 = expr2; } public boolean interpreter(String con) { return expr1.interpreter(con) || expr2.interpreter(con); }} public class AndExpression implements Expression {Expression implements expr1; Expression expr2; public AndExpression(Expression expr1, Expression expr2) { this.expr1 = expr1; this.expr2 = expr2; } public boolean interpreter(String con) { return expr1.interpreter(con) && expr2.interpreter(con); Public class Client {public static void main(String[] args) {Expression Person1 = new TerminalExpression("mick"); Expression person2 = new TerminalExpression("mia"); Expression isSingle = new OrExpression(person1, person2); Expression spike = new TerminalExpression("spike"); Expression mock = new TerminalExpression("mock"); Expression isCommitted = new AndExpression(spike, mock); System.out.println(isSingle.interpreter("mick")); System.out.println(isSingle.interpreter("mia")); System.out.println(isSingle.interpreter("max")); System.out.println(isCommitted.interpreter("mock, spike")); System.out.println(isCommitted.interpreter("Single, mock")); } // The output is true true false true falseCopy the code

Iii. Usage Scenarios

  • Recurring problems can use interpreter mode

  • A scenario where simple syntax needs to be explained, but some of the standard syntax transformations have been replaced by utility classes

Four advantages,

  • Easy to extend, modify the syntax, just need to change the response of the non-final expression, extend the syntax, just need to add a non-final expression class

Five, the disadvantages

  • Low execution efficiency

  • Interpreter patterns cause class inflation and are expensive to maintain

  • The interpreter pattern uses recursive invocation methods, where each expression needs to know the final result, and the layers must be stripped down