Interpreter pattern Photos:

Interpreter pattern
Photo:1
Interpreter pattern
Photo:2
Interpreter pattern
Photo:3
Interpreter pattern
Photo:4


Interpreter pattern Basic Informations:

Uses for the Interpreter pattern
2> Specialized database query languages such as SQL. Specialized computer languages which are often used to describe communication protocols. Most general-purpose computer languages actually incorporate several specialized languages. [edit]

Tags:Specialized Computer Language,Sql,
Example
2> The following Reverse Polish notation example illustrates the interpreter pattern. The grammar expression ::= plus | minus | variable | number plus ::= expression expression '+' minus ::= expression expression '-' variable  ::= 'a' | 'b' | 'c' | ... | 'z' digit = '0' | '1' | ... '9' number ::= digit | digit number defines a language which contains reverse Polish expressions like: a b + a b c + - a b + c a - - Following the interpreter pattern there is a class for each grammar rule. import java.util.Map; interface Expression { public int interpret(Map<String,Expression> variables); } class Number implements Expression { private int number; public Number(int number) { this.number = number; } public int interpret(Map<String,Expression> variables) { return number; } } class Plus implements Expression { Expression leftOperand; Expression rightOperand; public Plus(Expression left, Expression right) { leftOperand = left; rightOperand = right; } public int interpret(Map<String,Expression> variables) { return leftOperand.interpret(variables) + rightOperand.interpret(variables); } } class Minus implements Expression { Expression leftOperand; Expression rightOperand; public Minus(Expression left, Expression right) { leftOperand = left; rightOperand = right; } public int interpret(Map<String,Expression> variables) { return leftOperand.interpret(variables) - rightOperand.interpret(variables); } } class Variable implements Expression { private String name; public Variable(String name) { this.name = name; } public int interpret(Map<String,Expression> variables) { if(null==variables.get(name)) return 0; //Either return new Number(0). return variables.get(name).interpret(variables); } } While the interpreter pattern does not address parsing[2] a parser is provided for completeness. import java.util.Map; import java.util.Stack; class Evaluator implements Expression { private Expression syntaxTree; public Evaluator(String expression) { Stack<Expression> expressionStack = new Stack<Expression>(); for (String token : expression.split(" ")) { if (token.equals("+")) { Expression subExpression = new Plus(expressionStack.pop(), expressionStack.pop()); expressionStack.push( subExpression ); } else if (token.equals("-")) { // it's necessary remove first the right operand from the stack Expression right = expressionStack.pop(); // ..and after the left one Expression left = expressionStack.pop(); Expression subExpression = new Minus(left, right); expressionStack.push( subExpression ); } else expressionStack.push( new Variable(token) ); } syntaxTree = expressionStack.pop(); } public int interpret(Map<String,Expression> context) { return syntaxTree.interpret(context); } } Finally evaluating the expression "w x z - +" with w = 5, x = 10, and z = 42. import java.util.Map; import java.util.HashMap; public class InterpreterExample { public static void main(String[] args) { String expression = "w x z - +"; Evaluator sentence = new Evaluator(expression); Map<String,Expression> variables = new HashMap<String,Expression>(); variables.put("w", new Number(5)); variables.put("x", new Number(10)); variables.put("z", new Number(42)); int result = sentence.interpret(variables); System.out.println(result); } } [edit]

Tags:Class,
References
2> ^ Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. pp. 243 ISBN 0-201-63361-2 ^ Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. pp. 247 ISBN 0-201-63361-2 [edit]

Tags:Design Pattern,Design Patterns,Isbn 0-201-63361-2,
External links
2> The Wikibook Computer Science Design Patterns has a page on the topic of Interpreterimplementations in various languages Interpreter implementation in Ruby SourceMaking tutorial Interpreter pattern description from the Portland Pattern Repository v d e Design Patterns, the book Creational Abstract factory Builder Factory method Prototype Singleton Structural Adapter Bridge Composite Decorator Facade Flyweight Proxy Behavioral Chain of responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template method Visitor Retrieved from "http://en.wikipedia.org/w/index.php?title=Interpreter_pattern&oldid=473801455" Categories: Software design patternsArticles with example Java codeHidden categories: Articles needing additional references from November 2008All articles needing additional references Personal tools Log in / create account Namespaces Article Talk Variants Views Read Edit View history Actions Search Navigation Main page Contents Featured content Current events Random article Donate to Wikipedia Interaction Help About Wikipedia Community portal Recent changes Contact Wikipedia Toolbox What links here Related changes Upload file Special pages Permanent link Cite this page Print/export Create a bookDownload as PDFPrintable version Languages Български Deutsch Español Français Italiano 日本語 Polski Português Русский ไทย Українська This page was last modified on 29 January 2012 at 04:23. Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. See Terms of use for details. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.Contact us Privacy policy About Wikipedia Disclaimers Mobile view if ( window.isMSIE55 ) fixalpha(); if ( window.mediaWiki ) { mw.loader.load(["mediawiki.user", "mediawiki.util", "mediawiki.page.ready", "mediawiki.legacy.wikibits", "mediawiki.legacy.ajax", "mediawiki.legacy.mwsuggest", "ext.gadget.wmfFR2011Style", "ext.vector.collapsibleNav", "ext.vector.collapsibleTabs", "ext.vector.editWarning", "ext.vector.simpleSearch", "ext.UserBuckets", "ext.articleFeedback.startup", "ext.articleFeedbackv5.startup", "ext.markAsHelpful"]); } if ( window.mediaWiki ) { mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":1,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"highlightbroken":1,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":false,"showjumplinks":1,"shownumberswatching":1,"showtoc":1,"showtoolbar":1,"skin":"vector","stubthreshold":0,"thumbsize":4,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":1,"watchdefault":0,"watchdeletion":0,"watchlistdays":3,"watchlisthideanons":0, "watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"flaggedrevssimpleui":1,"flaggedrevsstable":0,"flaggedrevseditdiffs":true,"flaggedrevsviewdiffs":false,"vector-simplesearch":1,"useeditwarning":1,"vector-collapsiblenav":1,"usebetatoolbar":1,"usebetatoolbar-cgd":1,"wikilove-enabled":1,"variant":"en","language":"en","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"searchNs100":false,"searchNs101":false,"searchNs108":false,"searchNs109":false,"gadget-wmfFR2011Style":1});;mw.user.tokens.set({"editToken":"+\\","watchToken":false});;mw.loader.state({"user.options":"ready","user.tokens":"ready"}); /* cache key: enwiki:resourceloader:filter:minify-js:4:b41a86ec4e0fe8329bc3ce917e792339 */ }

Tags:Composite,Creational,Abstract Factory,Builder,Factory Method,Prototype,Singleton,Structural,Adapter,Bridge,Decorator,Facade,Flyweight,Proxy,Behavioral,Chain Of Responsibility,Command,Iterator,Mediator,Memento,Observer,State,Strategy,Template Method,Visitor,Categories,Software Design Patterns,


zote monety