Anzeige
Anzeige

Más contenido relacionado

Anzeige

Interpreter Design Pattern

  1. Interpreter Design Pattern
  2. 1- Review 2- Intent 3- Implements 4- Sample Code 5- Applicability 6- Consequences 7- Know Uses Overview
  3. 1- Review - What is Behavioral Pattern ? -> Design patterns that identify common communication patterns between object. - What is a common pattern?
  4. 2- Intent Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
  5. - The Implementation of the Interpreter pattern is just the use of the composite pattern applied to represent a grammar. - The Interpreter defines the behaviour while the composite defines only the structure. 3- Implements
  6. 3- Implements (Cont.)
  7. Abstract Expression - Declares an interface for executing an operator Terminal Expression - Implements an Interpret operation associated with terminal symbols in the grammar. - Interprets expressions containing any of the terminal tokens in the grammar. Nonterminal Expression - Interprets all of the nonterminal expressions in the grammar . Context - Contains the global information that is part of the parse. Client - Builds the syntax tree from the preceding expression types and invokes the interpret operation. 3- Implements (Cont.)
  8. public class Context { private string input; private int output; public Context (String input) { this.input = input; } public String getInput () { return input; } public void setInput(String input) { this.input = input; } public int getOutput () { return output; } public void setOutput (int output) { this.output = output; } } 4- Sample Code
  9. public abstract class Expression { public void interpret (Context context) { if (context.getInput().length() == 0) { return; } if (context.getInput().startsWith(nine)) { context.setOutput(context.getOutput() + (9 * multiplier() )); context.setInput(context.getInput().subString(2)); } else if (context.getInput().startsWith(four)) { context.setOutput(context.getOutput() + (4 * multiplier() )); context.setInput(context.getInput().subString(2)); } while (context.getInput().startsWith(one)) { context.setOutput(context.getOutput() + (4 * multiplier() )); context.setInput(context.getInput().subString(1)); } } 4- Sample Code (Cont.)
  10. public abstract String one(); public abstract String fout(); public abstract String nine(); public abstract int multiplier(); } 4- Sample Code (Cont.)
  11. The template method pattern should be used : - The interpreter pattern is used exhaustively in defining grammar, tokenize input and store it. - A specific area where Interpreter can be used are the rule engines. - The Interpreter pattern can be used to add functionality to the composite pattern. 5- Applicability
  12. - Easier to change and extend the grammar - Implementing the grammar is easily - Adding new ways to interpret expressions - Lets you embed a language into program - Complex grammars are hard to maintain 6- Consequences
  13. - Python uses the Interpreter pattern to generate byte code for a parse tree. - A scheme interpreter pattern, directly executes the parse tree, making small optimizations as it goes along. - Text editors and Web browsers use the interpreter pattern to lay out document and check spelling. 7- Know Uses
  14. Thanks !
Anzeige