Applied Design Patterns with Java

Behavioral :: Interpreter (243) {C ch 18}


Example - Java :: Patterns\Behavioral\Interpreter

Pattern Concept: to define a grammar for a language, and use that grammar to interpret statements in the language. The Interpreter pattern drives three use cases:

  1. When a command Interpreter is needed to parse user commands that allow a query facility that returns different results, based on the parsed input.
  2. When a program must parse an algebraic string, a common occurrence in math and graphics packages.
  3. When a program must produce varied output different from how the input is stored or represented, i.e.: like a Report Generator, or Traffic Analyzer, etc.

Interpreting a language takes three steps:

  1. parsing language symbols into tokens
  2. reducing the tokens into actions
  3. executing the actions

Examples - UML : SimpleParse and InterpDemo

Here is a sample parse output, with a diagram showing the parse reduction, and the representive classes to accomplish this in UML:


Here is Cooper's Class Diagram for the application using this logic, followed by the Rose equivalent:





The example Java program is called 'InterpDemo'.

The UML diagram is above, and the list of Java files is below:

Issues and consequences of the Interpreter pattern include:

Catalog Behavioral Prev Next