Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Interpreter design pattern.
The Interpreter design pattern is a behavioral design pattern that provides a way to interpret sentences or phrases in a defined language. If certain sentences and phrases are used somewhere in the program and you need a solution to analyze, understand and calculate those sentences and phrases, the Interpreter design pattern will be a useful algorithm.
This template consists of the following components:

The Interpreter pattern can be used to define a language for a specific domain and provides a way to evaluate sentences or expressions in that language. It is useful when you need to parse and interpret data structures or complex expressions, such as in compilers or query languages.
Examples of this pattern in real-world applications include SQL query interpreters, regular expression matchers, and programming language interpreters. In the following, we will understand this pattern better with a simple example.
Suppose we want to make a simple calculator that can perform the four basic operations of addition, subtraction, multiplication and division. We can use the Interpreter pattern to display these expressions and evaluate them.
First, we define the abstract expression interface:
public interface IExpression
{
int Interpret();
}In the next step, we define an expression from the group of basic expressions that include expressions that cannot be divided into smaller parts to represent numbers:
public class NumberExpression : IExpression
{
private readonly int _number;
public NumberExpression(int number)
{
_number = number;
}
public int Interpret()
{
return _number;
}
}You can see that this expression, being the basic reason, does not need any special operation to interpret and just returns the value. Then, we define the four main expressions for addition, subtraction, multiplication and division operations:
public class AddExpression : IExpression
{
private readonly Expression _leftExpression;
private readonly Expression _rightExpression;
public AddExpression(Expression leftExpression, Expression rightExpression)
{
_leftExpression = leftExpression;
_rightExpression = rightExpression;
}
public override int Interpret()
{
var leftOperand = _leftExpression.Interpret();
var rightOperand = _rightExpression.Interpret();
return leftOperand + rightOperand;
}
}
public class SubtractExpression : Expression
{
//Similar to AddExpression Class
}
public class MultiplicationExpression : Expression
{
//Similar to AddExpression Class
}
public class DivisionExpression : Expression
{
//Similar to AddExpression Class
}Finally, we define the usage code that creates and executes the expressions:
public static void Main()
{
// Create the expression tree: ((3 + (4 * 5)) - (6 / 2))
var expression = new SubtractExpression(
new AddExpression(
new NumberExpression(3),
new MultiplicationExpression(
new NumberExpression(4),
new NumberExpression(5))),
new DivisionExpression(
new NumberExpression(6),
new NumberExpression(2)));
// Evaluate the expression
var result = expression.Interpret();
Console.WriteLine($"Result: {result}");
}In this example, we first create an expression that expresses the expression 3+4*5-6/2 and calculate it using the Interpret method. Note that this is a simple example. The Interpreter pattern can be used to define much more complex languages and expressions.
To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.
I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.
Where is the 'Expression' class declared, that the non-terminal expressions inherit from?
Sorry for the mistake. It was a typo and has been corrected. Thank you.
leave a comment