Interpreter Pattern

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language

Interpreter Pattern Diagram

Applicability

  • The grammar is simple. For complex grammars, the class hierarchy for the grammar becomes large and unmanageable. Tools such as parser generators are a better alternative in such cases. They can interpret expressions without building abstract syntax trees, which can save space and possibly time
  • Efficiency is not a critical concern. The most efficient interpreters are usually not implemented by interpreting parse trees directly but by first translating them into another form. For example, regular expressions are often transformed into state machines. But even then, the translator can be implemented by the Interpreter pattern, so the pattern is still applicable

#DesignPatterns

Let's code up a really simple application to demonstrate the usage of this pattern. We want to define some rules upfront to assure what kind of sentence valid. Firstly, let's define an interface Interpreter

Interpreter Interface

Next up, we want to implement a Match Interpreter struct that check whether the string is match the name defined at run time

Interpreter Match Struct

Finally, we implement a checkName function that create a match interpreter as below

Interpreter Creator

And there we have it - the Interpreter Pattern implemented in Go. This pattern is the least widely used by most of software engineer as it is dealing with the language gramma and syntax, this is not most of the application in the world are (typical example would be SQL)

P/s: The accompany code is included in this PR 😊

Happy coding 😎