The interpreter mode of the design pattern

Source: Internet
Author: User

The interpreter pattern definition: Given a language, defines a representation of its grammar and defines an interpreter that uses that representation to interpret sentences in the language.

Advantages:

Interpreter is a simple parsing tool, its most significant advantage is extensibility, modify the grammar rules as long as the corresponding non-terminator expression can be modified, if the extension of the syntax, as long as the addition of non-Terminator class can be.

Disadvantages:

1, the interpreter mode will cause the class to swell.

2, the interpreter mode uses the recursive call method.

3, efficiency issues.

The class diagram is as follows:


The implementation code is as follows:

An abstract expression class:

Package Com.designpatterns.interpreter;import java.util.hashmap;/** * @author WSYW126 * @version created: May 8, 2016 PM 4 : 28:15 * Class Description: Alljava */public abstract class Abstractexpression {public abstract int interpreter (hashmap<string, Integ er> var);}

Variable Interpreter class:

Package Com.designpatterns.interpreter;import java.util.hashmap;/** * @author WSYW126 * @version created: May 8, 2016 PM 4 : 31:09 class Description: Alljava */public class Varexpression extends Abstractexpression {private String key;public varexpression ( String key) {This.key = key;} @Overridepublic int Interpreter (hashmap<string, integer> var) {return var.get (This.key);}}

Abstract operator Interpreter class:

Package com.designpatterns.interpreter;/** * @author WSYW126 * @version created: May 8, 2016 4:33:55 class Description: Alljava */public ABS Tract Class Symbolexpression extends Abstractexpression {protected abstractexpression left;protected Abstractexpression right;public symbolexpression (abstractexpression left, abstractexpression right) {this.left = left; This.right = Right;}}

Addition Interpreter:

Package Com.designpatterns.interpreter;import java.util.hashmap;/** * @author WSYW126 * @version created: May 8, 2016 PM 5:0 5:49 * Class Description: Alljava */public class Addexpression extends Symbolexpression {public addexpression (abstractexpression left, A Bstractexpression right) {super (left, right);} @Overridepublic int Interpreter (hashmap<string, integer> var) {return Super.left.interpreter (Var) + Super.right.interpreter (Var);}}

Addition Interpreter:

Package Com.designpatterns.interpreter;import java.util.hashmap;/** * @author WSYW126 * @version created: May 8, 2016 PM 5:0 4:45 * Class Description: Alljava */public class subexpression extends Symbolexpression {public subexpression (abstractexpression left, A Bstractexpression right) {super (left, right);} @Overridepublic int Interpreter (hashmap<string, integer> var) {return Super.left.interpreter (Var)- Super.right.interpreter (Var);}}

Parser Encapsulation Class:

Package Com.designpatterns.interpreter;import java.util.hashmap;import java.util.stack;/** * @author WSYW126 * @ Version creation time: May 8, 2016 5:06:26 class Description: Alljava */public class Calculator {private abstractexpression ae;public Calculator ( String expstr) {stack<abstractexpression> s = new stack<abstractexpression> (); char[] Chararray = Expstr.tochararray (); Abstractexpression left = null; Abstractexpression right = null;for (int i = 0; i < Chararray.length, i++) {switch (Chararray[i]) {case ' + ': left = S.po P (); right = new Varexpression (string.valueof (chararray[++i)); S.push (new Addexpression (left, right)); Break;case '-': left = S.pop (), right = new Varexpression (string.valueof (Chararray[++i])), S.push (new subexpression (left, right)); ;d Efault:s.push (New Varexpression (String.valueof (chararray[i))); This. AE = S.pop ();} public int Run (hashmap<string, integer> var) {return this. Ae.interpreter (Var);}}

Test class:

Package Com.designpatterns.interpreter;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import java.util.hashmap;/** * @author WSYW126 * @version created: May 8, 2016 5:14:28 Class Description: Alljava */public class Client {public static void main (string[] args) throws IOException {String expstr = Getexpstr (); Hashmap<string, integer> var = getValue (EXPSTR); Calculator cal = new Calculator (EXPSTR); SYSTEM.OUT.PRINTLN ("The result of the operation is:" + expstr + "=" + Cal.run (Var));} private static hashmap<string, integer> GetValue (String expstr) throws IOException {hashmap<string, Integer > map = new hashmap<string, integer> (), for (Char Ch:expStr.toCharArray ()) {if (ch! = ' + ' && ch! = '-') {if (!map.containskey (string.valueof (CH))) {String in = (new BufferedReader (New InputStreamReader (system.in))). ReadLine (); Map.put (string.valueof (CH), integer.valueof (in));}} return map;} private static String Getexpstr () throws IOException {System.out.println ("Enter evaluated expression:"); reTurn (new BufferedReader (New InputStreamReader (system.in))). ReadLine ();}} 

This is the interpreter mode.


References :
The Zen of design pattern

Remark :
Reprint Please specify source: http://blog.csdn.net/wsyw126/article/details/51336078
WSYW126

The interpreter mode of the design pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.