Interpreter interpreter mode (behavioral mode)
Motive (motivation)
In the software construction process, if a particular area of the problem is more complex, similar patterns are repeated, if the use of ordinary programming to achieve will face very frequent changes.
In this case, the problem of a particular domain is expressed as a sentence under some grammatical rule, and then an interpreter is constructed to interpret such a sentence, thus achieving the goal of solving the problem.
Intentions (Intent)
Given a language, define a representation of its grammar and define an interpreter, which is used to interpret sentences in a language. --"Design pattern" GoF
Convert Chinese numerals to Arabic numerals
Public classContext {Private stringstatement; Private intdata; PublicContext (stringstatement) { This. Statement =statement; } Public stringStatement {Get { returnstatement; } Set{statement=value; } } Public intData {Get { returndata; } Set{Data=value; } } } Public Abstract classExpression {protecteddictionary<string,int> table =Newdictionary<string,int>(); PublicExpression () {table. ADD ("a",1); Table. ADD ("two",2); Table. ADD ("three",3); Table. ADD ("Four",4); Table. ADD ("Five",5); Table. ADD ("Six",6); Table. ADD ("Seven",7); Table. ADD ("Eight",8); Table. ADD ("Nine",9); } Public Virtual voidinterpret (context context) {if(Context. Statement.length = =0) { return; } foreach(stringKeyinchtable. Keys) {intValue =Table[key]; if(Context. Statement.endswith (key +Getpostfix ())) {context. Data+ = value *Multiplier (); Context. Statement= Context. Statement.substring (0, context. Statement.length-getlength ()-1); } if(Context. Statement.endswith ("0") {context. Statement= Context. Statement.substring (0, context. Statement.length-1); } } } Public Abstract stringGetpostfix (); Public Abstract intMultiplier (); Public Virtual intGetLength () {return This. Getpostfix (). Length; } } Public classgeexpression:expression { Public Override stringGetpostfix () {return string. Empty; } Public Override intMultiplier () {return 1; } Public Override intGetLength () {return 0; } } Public classshiexpression:expression { Public Override stringGetpostfix () {return "10"; } Public Override intMultiplier () {return Ten; } } Public classbaiexpression:expression { Public Override stringGetpostfix () {return "Hundred"; } Public Override intMultiplier () {return -; } } Public classqianexpression:expression { Public Override stringGetpostfix () {return "thousand"; } Public Override intMultiplier () {return +; } } Public classwanexpression:expression { Public Override stringGetpostfix () {return "million"; } Public Override intMultiplier () {return 10000; } Public Override voidinterpret (context context) {if(Context. Statement.length = =0) { return; } List<Expression> tree =NewList<expression>(); Tree. ADD (Newgeexpression ()); Tree. ADD (Newshiexpression ()); Tree. ADD (Newbaiexpression ()); Tree. ADD (Newqianexpression ()); foreach(stringKeyinchtable. Keys) {if(Context. Statement.endswith ( This. Getpostfix ())) {inttemp =context. Data; Context. Data=0; Context. Statement= Context. Statement.substring (0, context. Statement.length-1); foreach(Expression expinchtree) {exp. Interpret (context); } context. Data= temp + This. Multiplier () *context. Data; } } } }
Client calling Code:
Static voidMain (string[] args) { stringRoman ="10,503,602"; Context Context=NewContext (roman); List<Expression> tree =NewList<expression>(); Tree. ADD (Newgeexpression ()); Tree. ADD (Newshiexpression ()); Tree. ADD (Newbaiexpression ()); Tree. ADD (Newqianexpression ()); Tree. ADD (Newwanexpression ()); foreach(Expression expinchtree) {exp. Interpret (context); } Console.WriteLine ("{0} = {1}", Roman, context. Data); Console.readkey (); }
Output:
10,503,602 = 10503602
This code is to convert the Chinese numbers to Arabic numerals, if the process-oriented algorithm is definitely to intercept the string to be judged and then combined into the correct number. In this code, the interpreter pattern is used to encapsulate the number of bits in the expression.
First look at the abstract expression class, first it encapsulates a dictionary class, this dictionary holds the Chinese 1-9 characters and their corresponding Arabic numerals. The interpret method iterates through each Chinese digit in the dictionary and determines whether the truncated Chinese digit and suffix is the end, and if so, multiplies the corresponding value by the corresponding number of digits, and the corresponding bit number is a pure virtual function multiplier, which is implemented by the derived class itself. After you have obtained the value of the corresponding expression, you need to remove the corresponding string from the Chinese number statement.
In Wanexpression, we rewrote the interpret function, which is because the million digits can be described by numbers below the thousand, such as 32 million. The algorithm of overriding the interpret function is as follows, the following expressions (including thousands) are added to a list to save, and then traverse the number table, if the current expression has been intercepted million, then the obtained value is saved with intermediate temporary variables, the current expression of the numeric value is set to 0. Then the text is intercepted. Then use the list of thousands, hundred, ten, and the expression to explain, the last obtained value multiplied by the corresponding number of digits plus temporary variables.
In the main function, a list is used to save an implementing expression, which in turn is used to parse the context. Note that the order of the parsing is a implementing order.
Structure (Structure)
Several points of interpreter model
- The application of interpreter mode is a difficult problem in the application of interpreter mode, it is suitable to use interpreter mode only if it satisfies the question that "business rules change frequently, and similar patterns are repeated and easily abstracted into grammatical rules".
- Use the interpreter pattern to represent grammar rules so that you can use object-oriented techniques to facilitate "extended" grammars.
- The interpreter model is suitable for simple grammatical representations, and for complex grammatical representations, the interpreter pattern produces a larger class hierarchy and requires recourse to standard tools such as the parser generator.
Reprint please specify the source:
Jesselzj
Source: http://jesselzj.cnblogs.com
Design mode 15:interpreter interpreter mode (behavioral mode)