2006.6.1 Li Jianzhong
Motivation)
In the process of software building, if the problems in a specific field are complicated and similar models are repeated, the implementation of common programming methods will face frequent changes.
In this case, the problem in a specific field is expressed as a sentence under a certain syntax rule, and then an interpreter is constructed to explain such a sentence, so as to solve the problem.
Intent)
Given a language, it defines a syntax expression and an interpreter that uses this representation to interpret sentences in the language.
-- Design Pattern GoF
For example, Interpreter Application
Assume that you want to write a program to convert Chinese characters to numbers.
If we can break it down into every small part to solve the problem, it will be much easier.
Context, statement is an unprocessed string, and data is the processed result.
Interpret is the interpreter and the core of Expression.
Items
10
For tens of thousands, it is complicated, because tens of thousands can be thousands, hundreds, and tens of thousands. Therefore, he needs to include the Processing Methods of the first.
Customer Program
The order of tree addition must be 10 million. 0.1 billion is similar to. If needed, you can directly extend the Expression by without changing the original class. This is the beauty of the Interpreter mode.
Structure)
In the preceding example, Context is the Context of grammar processing. AbstractExpression corresponds to Expression, and TerminalExpression terminal expressions correspond to a series of geexpressions and shiexpressions. NonterminalExpression is a non-terminal expression. It is not written in this example. It can be used in combination with the tree structure of the combination mode, because a large expression is sometimes combined by a small expression.
Key Points of Interpreter Mode
Interpreter mode is a difficult Application Scenario in Interpreter mode. Only when "business rules change frequently and similar models are repeated, and it is easy to abstract into syntax rules. "This is suitable for Interpreter mode.
The Interpreter mode is used to represent grammar rules, so that you can use object-oriented techniques to easily "extend" grammar.
The Interpreter mode is suitable for simple syntax representation. For complex syntax representations, the Interpreter mode produces a large class hierarchy. Therefore, you need to turn to standard tools such as syntax analysis generators.
Interpreter in. NET Architecture
A regular expression is a typical interpreter. In ASP. NET, when the aspx file is converted to a dll, the html language is processed. This process also contains the interpreter mode. The Interpreter mode actually has the shadow of the Composite mode, but they solve different problems.
2010.10.12