0. Interpreter mode definition:
Given a language, define a representation of its grammar, and define an interpreter that uses the representation to interpret sentences in the language. Belongs to behavioral mode.
The interpreter mode is used very little in actual system development because it can cause problems such as efficiency, performance, and maintenance.
The general class diagram of the interpreter mode is shown in the figure.
1. Advantages of Interpreter Mode
The interpreter is a simple syntax analysis tool. Its most prominent advantage is its expansion. Modifying the syntax rules is just to modify the corresponding non-terminal expression. If you expand the syntax, you just need to add a non-terminal class.
2. Disadvantages of Interpreter Mode
The interpreter pattern will cause class expansion: each syntax needs to produce a non-terminal expression. When the syntax rules are relatively complex, a large number of class files may be generated, which brings a lot of trouble to maintenance.
The interpreter pattern adopts a recursive call method: each non-terminal expression only cares about the expressions related to itself. Each expression needs to know the final result and must be uncoated layer by layer. Whether it is a process-oriented language or an object-oriented language, recursion is used under necessary conditions, which leads to very complex debugging. Think about it, if we want to troubleshoot a syntax error, do we need to debug one by one, until the smallest syntax unit?
Efficiency problem: The interpreter mode uses a lot of loops and recursion, efficiency is an issue that cannot be ignored, especially when used to parse complex and lengthy syntax. Efficiency is unbearable.
3. Use scenarios of interpreter mode
Repeated problems can be used with the interpreter mode: for example, multiple application servers generate a large number of logs every day, and the log files need to be analyzed and processed. Since the log formats of each server are different, the data elements are the same. According to the interpreter, the terminator expressions are the same, but non-terminator expressions need to be formulated. In this case, the problem can be solved once and for all through a program.
A scenario where simple syntax needs to be explained: Why is it simple? Look at non-terminal expressions. The more grammatical rules, the higher the complexity, and recursive calls are required between classes (see the stack in our example), which is not generally complicated. Think about it, what kind of patience and confidence do you need to troubleshoot problems when calling multiple classes. Therefore, the interpreter mode is generally used to parse comparative standard character sets, such as SQL syntax analysis, but this part is gradually replaced by specialized tools. In some special business environments, the interpreter model will also be used. The example we just did is a business environment, and there are many examples of model operations now. Many commercial institutions are able to provide a large amount of data for analysis.
4. Simple examples
/** * Declare an abstract interpretation operation*/ public interface Interpreter { public void interpret(Context context); //In fact, there can be a returned type to define the interpreted data object} public class XmlSaxInterpreter implements Interpreter { @Override public void interpret(Context context) { System.out.println("xml sax Interpreter:" + context.getData()); } } public class XmlDomInterpreter implements Interpreter { @Override public void interpret(Context context) { 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) { Context context = new Context(); context.setData("a piece of xml data"); new XmlSaxInterpreter().interpret(context); new XmlDomInterpreter().interpret(context); } }5. Notes on interpreter mode
Try not to use interpreter mode in important modules, otherwise maintenance will be a big problem. In the project, you can use script languages such as shell, JRuby, and Groovy to replace the interpreter mode to make up for the shortcomings of Java compiled languages. We use JRuby for calculation processing in a bank's analytical project, avoiding the four-character operation in the interpreter mode, and performing well in all aspects of efficiency and performance.