Interpreter

Behavioral pattern

The Interpreter design pattern is used for evaluating a determined language and return an Expression. This pattern can interpret languages such as Java, C#, SQL, or even a new one invented by us, and provide a response based on its evaluation.

This is one of the most complex design patterns, because we need to combine various object-oriented programming techniques in order to implement it, which can make it a bit difficult to understand. The main techniques we're going to be dealing with are Inheritance, Polymorphism and Recursivity.

Interpreter design pattern structure.
Interpreter design pattern structure.

The components included in the Interpreter pattern are as follows:

  • Client: Actor triggering the execution of the interpreter.
  • Context: Object carrying global information to be used by the interpreter in order to read and store information of all the classes in the pattern. This object is sent to the interpreter which then relays it to the rest of the structure.
  • AbstractExpression: Interface for defining the basic structure of an expression.
  • TerminalExpression: It refers to expressions which, after being evaluated or interpreted, end the execution of a branch or subtree.
  • NonTerminalExpression: These are composite expressions, which contain more expressions subject to evaluation. They are interpreted by using recursivity until a terminal expression is found.
Interpreter pattern sequence diagram.
Interpreter pattern sequence diagram.
  1. The client creates the context for the execution of the interpreter.
  2. The client creates or gets the expression to be evaluated.
  3. The client requests for the evaluation of the expression to the interpreter . To that end, it sends it the context.
  4. The Expression calls the Non-terminal Expressions .
  5. The Non-terminal Expressions call all the Terminal Expressions.
  6. The Root Expression requests for the interpretation of a Terminal Expression.
  7. The Expression is completely evaluated and provides the result of the interpretation of all the Terminal Expressions and Non-terminal Expressions.

Real-world example

By implementing the Interpreter design pattern, we are going to build an application capable of interpreting SQL commands to send queries to an Excel file, as if it were a relational database, where each sheet would be seen as a chart, and the columns of that sheet would be seen as the columns of a chart. To that end, we will build our own class structure for representing the SQL language, in order to be then interpreted and provide a result.

Discover how the Interpreter pattern can help us solve this problem.
Discover how the Interpreter pattern can help us solve this problem.

About this book

Introduction to design patterns

Everything you just saw in this article is only a small part of the book Introduction to design patterns, the most complete book of design patterns in Spanish, we cover 25 design patterns along with 25 real-world projects. Forget about learning design patterns with typical Internet examples of how to make pizza, animals, and geometric shapes. I invite you to see my book:

See book
All rights reserved ©
Reactive programming
LinkedinYoutubeTwitterFacebook

© 2021, Copyright - Oscar Blancarte. All rights reserved.