Interpreter Design Pattern
Learn how to implement the Interpreter design pattern in Kotlin.
We'll cover the following...
This design pattern may seem very simple or very hard, based on how much background someone has in computer science. Some books that discuss classical software design patterns even decide to omit it altogether or put it somewhere at the end, for curious readers only. The reason behind this is that the Interpreter design pattern deals with translating specific languages. But why would we need that? Don’t we have compilers to do that anyway?
We need to go deeper
All developers have to speak many languages or sub-languages. Even as regular developers, we use more than one language. Think of the tools that build our projects, such as Maven or Gradle. We can consider their configuration files and build scripts languages with specific grammar. If we put elements out of order, our project won’t be built correctly. This is because such projects have interpreters to analyze configuration files and act upon them.
Other examples are query languages, whether one of the SQL variations or one of the languages specific to NoSQL databases. An Android developer, may think of XML layouts as such languages too. Even HTML could be considered as a language that defines user interfaces. And there are others, of course.
Maybe you’ve worked with one of the testing frameworks that defines a custom language for testing, such as Cucumber. Each of these examples can be called a domain-specific ...