C#

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 : Expression
{
   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.

Last updated