Using Interpreter Design Pattern In Java
The Interpreter Design Pattern is one of the Gang of Four design patterns which specifies how to evaluate sentences in a language.
Join the DZone community and get the full member experience.Join For Free
Here I am with another design pattern to discuss — Interpreter Design Pattern.
Interpreter Design Pattern
- The Interpreter Design Pattern is one of the Gang of Four design patterns which specifies how to evaluate sentences in a language.
- The Interpreter pattern is a behavioral pattern that provides a way to evaluate language grammar or expression.
- The Interpreter pattern uses an expression interface that tells how to interpret a particular context.
- The Interpreter pattern is used to define a grammatical representation for a language and also provides an interpreter to deal with the grammar.
- The basic idea of the Interpreter pattern is to have a class for each symbol:
- Terminal - are the elementary symbols of the language defined by a formal grammar.
- NonTerminal - also known as syntactic variables that are replaced by groups of terminal symbols according to the production rules. The NonTerminal uses a composite design pattern in general.
- A grammar is defined by production rules that specify which symbols may replace which other symbols. The production rules or simply called productions may be used to generate and parse strings.
- SQL interpreter is a good example of this pattern.
- Language interpreters are another great example of this.
- I have also used the Interpreter pattern in the example of the Command Design Pattern for interpreting the appliance and its operation to perform in the command string.
Employee Data Management Using Interpreter Design Pattern
Let's create a class for storing Employee data:
Here's the code for the Engineer class:
Here's the code for the Manager class:
Here's the code for Department enum:
Now, let's start implementing the Expression pattern. First, we define the interface for Expression:
Here's the code for TerminalExpression class:
Here's the code for NonTerminalExpression class:
I am using only TerminalExpression in the example and NonTerminalExpression is provided for reference and enhancement at your end.
Here's the code for NameExpression class:
Here's the code for DesignationExpression class:
Here's the code for DepartmentExpression class:
Here's the code for ManagerOfExpression class:
Here's the code for SalaryExpression class:
Additionally, I am writing two more classes to combine expressions via 'And' or 'Or'.
Here's the code for AndExpression class:
Similarly, here's the code for OrExpression class:
Again, I am using AndExpression only and I am providing OrExpression for your reference to use in your use case.
Now let write a class to parse the expression. Here I am using the below format for the expression.
Having a pattern for parsing is recommended and that offers easy implementation, maintains an understanding of the expression. The format defines the grammar of the expression which is needed for the accurate evaluation of the expression.
Now since we are agreed on the format for expression, let's write a class for parsing the expression and create an Expression object. Writing parser is not part of the Interpreter Pattern and provided only for completing the code and example.
Expression format and Parser are tightly coupled and we have to modify or use a different parser if we do change the expression format.
Here's the code for ExpressionParser class:
We can see that each expression concrete class is bound with a keyword to decode and evaluate.
Now since our, all component is ready to use for Expression pattern, let's write our Main class to execute and test the output:
And below is the output of the program:
The Source Code can be found here: Interpreter-Design-Pattern-Sample-Code
I hope this tutorial helps in the understanding of the Interpreter design pattern.
Liked the article? Please don't forget to press that like button. Happy coding!
Opinions expressed by DZone contributors are their own.