Design patterns: Interpreter

I am just as I promised after two weeks and as I promised I will be describing the interpreter design pattern, as you can guess it interprets some content on the content we want, ie it is used in translators and compilers, etc. more precisely in the article.



  • Interpretation of specific expressions, e.g. to a specific programming language.
  • Defining the description of the grammar of the interpreted language.



The problem in which the interpreter can be used may relate to domains that relate to languages that we do not want to translate ourselves on each occasion, so we can create an interpreter for them that will translate their grammar itself.


Use when:

  • You want to create your own compiler
  • You create a project that has functionalities that need to be interpreted into a language that the user understands.
  • Some tasks in the project are written in the interpreted language and there exists a grammar to it.



Each abstract base class of the interpreter defines the interpret() method, and each concrete class inheriting from the base class implements the interpret() method, which translates the specific part of the language required at the moment.

As you can see, understanding the use of the interpreter pattern is easy, but its implementation is a little more difficult, the description of the implementation and the pattern structure further in the article.



As you can see in the UML diagram below, the interpreter consists of:

  • The Context class that holds the data being interpreted.
  • The AbstractExpression class, which interprets the commands.
  • Specific classes that inherit from the AbstractExpression class, which interpret Context for individual cases.

Scheme of Interpreter

The schema in the interpreter pattern code looks like this:

The UML diagram of the interpreter pattern can also look like this:

Interpreter Pattern UML Diagram

The only difference is that there is no Context class, the code looks like this:

In this example, we divide the characters, the numbers we put on the stack, when we want to add them or remove them, we subtract the numbers from the stack one by one, we get the result:


Real-life examples

An example of this is the musical keyboard, which interprets clicks on keys for specific tones and notes.

Example of Interpreter


Roman numeral system

The real-life example that we will process in the code is based on the Roman numeral system, which we will interpret to the decimal system, that is, the numerical system that we use every day.

This is how it looks in the code:

It looks like in the previous examples, in the client we create and save specific classes to the list and interpret them in the foreach loop one by one. The main Interpret() method is in the Expression class, which interprets all Roman characters and calculates them into decimal values. Analyze this code thoroughly, you will not learn anything by looking at examples 😐

The results are:


Relations with other design patterns

  • The abstract Interpreter syntax tree is similar to the composite leaf tree.
  • The interpreter can use the iterator to traverse the structure.
  • You can use the Flyweight to divide symbols in the abstract syntax tree of the interpreter.
  • The Visitor can manage the behavior at each node interpreter tree.



That’s all philosophy about the interpreter pattern 🙂

Link to github with the whole code from this article:

This content also you can find on my steemit blog

And on medium

I am aware that a lot of these patterns and it is difficult to understand all of them, but come back to them sometimes as you will have to solve a problem that can be solved with some pattern there is probably no one or there are very few people who remember all patterns 🙂

In the next article, we will talk about the Iterator pattern.

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb: Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

If that post was useful for you share it with your friends :)

Post a comment

Be first!

Notify about