0. Interpreter (interpreter) mode definition:
given a language, defines a representation of its grammar, and defines an interpreter that uses the representation to interpret a sentence in a language. belongs to the behavioral pattern.
The interpreter pattern is rarely used in actual system development because it can cause problems with efficiency, performance, and maintenance.
The generic class diagram of the interpreter pattern is shown in the figure.
1. The advantages of the interpreter model
Interpreter is a simple syntax analysis tool, its most significant advantage is extensibility, modify the grammar rules as long as the corresponding non-terminal expression can be modified, if extended syntax, it is only to increase the Non-terminal class.
2. Shortcomings of the interpreter model
The interpreter pattern can cause class bloat: Each syntax produces a non-terminal expression, and when the syntax rules are more complex, a large number of class files can be generated, causing a lot of trouble for maintenance.
The interpreter pattern uses a recursive invocation method: Each non-terminal expression is concerned only with expressions of its own concern, each expression needs to know the final result, must peel the cocoon layer by layer, whether it is a process-oriented language or an object-oriented language, recursion is used under the necessary conditions, which results in very complex debugging. Think about it, if you want to troubleshoot a grammatical error, we are not going to have to debug a breakpoint until the smallest syntax unit.
Efficiency problem: Interpreter mode because of the use of a large number of loops and recursion, efficiency is a problem that can not be ignored, especially for the resolution of complex, lengthy syntax, the efficiency is unbearable.
3. Usage scenario for interpreter mode
Recurring problems can be used in interpreter mode: For example, multiple application servers generate a large number of logs every day, need to parse the log file, because the log format of each server is different, but the data elements are the same, according to the interpreter is the non-terminal expression is the same, But non-non-terminal expressions need to be developed. In this case, the program can be used to solve the problem once and for all.
A simple grammar needs to explain the scene: Why is it simple? Look at the non-final expression, the more grammar rules, the higher the complexity, and the recursive call between classes (see the stack in our example), not generally complex. Think about the calls between multiple classes what kind of patience and confidence you need to troubleshoot problems. Thus, the interpreter pattern is typically used to parse a standard character set, such as SQL parsing, but the part is gradually replaced by a dedicated tool. In some particular business environment will also adopt the interpreter mode, we just have a business environment, and now there are so many examples of model operations, many business organizations have been able to provide a large number of data for analysis.
4. Simple example
/** * Declares an abstract interpretation operation/public interface interpreter {public void interpret (context);//In practice, you can have a return type, set The interpreted data Object} public class Xmlsaxinterpreter implements interpreter {@Override public void interpret (context con
Text) {System.out.println ("XML SAX Interpreter:" + context.getdata ()); } public class Xmldominterpreter implements interpreter {@Override public void interpret
{System.out.println ("XML DOM Interpreter:" + context.getdata ());
}/** * Contains some information outside the interpreter/public class Context {private String data;
Public String GetData () {return data;
public void SetData (String data) {this.data = data;
} public class Test {public static void main (string[] args) {The context context = new context ();
Context.setdata ("A piece of XML data");
New Xmlsaxinterpreter (). interpret (context);
New Xmldominterpreter (). interpret (context);
}
}
5. Considerations for the Interpreter model
Try not to use interpreter patterns in important modules, or maintenance can be a big problem. In your project, you can replace the interpreter pattern with Shell, JRuby, groovy, and other scripting languages to make up for the lack of Java-compiled language. We use JRuby in a Bank analytics project to avoid the use of interpreter mode arithmetic, efficiency and performance in all aspects of the good performance.