Interpreter< Computer Science Design Patterns
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
The following Reverse Polish notation example illustrates the interpreter pattern. The grammar:
expression ::= plus | minus | variable | number defines a language which contains reverse Polish expressions like:
plus ::= expression expression '+'
minus ::= expression expression '-'
variable ::= 'a' | 'b' | 'c' | ... | 'z'
digit ::= '0' | '1' | ... '9'
number ::= digit | digit number
a b + Following the interpreter pattern there is a class for each grammar rule.
a b c + -
a b + c a - -
This pattern is not expensive. It dramatically reduces the business code so there is only few code to handle.
If the code already exists, this pattern is a little expensive.
This pattern is very easy to maintain. There is no additional cost due to the pattern.
This pattern is easy to remove with refactoring operations from your IDE.
- Use the
Interpreterterm to indicate the use of the pattern to the other developers.