Here, we will see how to create Interpreter Design Pattern with example.
The Interpreter Design Pattern is a behavioral design pattern that provides a way to define a language's grammar and an interpreter for that grammar. This pattern is particularly useful when you need to evaluate expressions and rules in a specific language or domain.
Example
A common real-world application of the Interpreter Pattern is in SQL query
interpreters. SQL statements can be represented as abstract syntax trees, where
each node represents different SQL operations such as SELECT, FROM, WHERE, etc.
The interpreter evaluates these trees to execute the queries against a database.
We will explain in detail lator part of the article with
code example.
This will evaluate simple mathematical expression. Let's create a simple example where we interpret mathematical expressions consisting of numbers and addition operations.
This block of code demonstrates the use of the Interpreter Pattern to evaluate a simple mathematical expression like (5 + 3).
using System;
// Abstract Expression
public interface IExpression
{
int Interpret();
}
// Terminal Expression for Numbers
public class Number : IExpression
{
private int _number;
public Number(int number)
{
_number = number;
}
public int Interpret()
{
return _number;
}
}
// Non-Terminal Expression for Addition
public class Addition : IExpression
{
private IExpression _leftExpression;
private IExpression _rightExpression;
public Addition(IExpression left, IExpression right)
{
_leftExpression = left;
_rightExpression = right;
}
public int Interpret()
{
return _leftExpression.Interpret() + _rightExpression.Interpret();
}
}
class Program
{
static void Main(string[] args)
{
// Constructing the expression: (5 + 3)
IExpression expression = new Addition(new Number(5), new Number(3));
// Interpreting the expression
Console.WriteLine($"Result: {expression.Interpret()}"); // Outputs: Result: 8
}
}
Outtput
A common real-world application of the Interpreter Pattern is in SQL query interpreters. SQL statements can be represented as abstract syntax trees, where each node represents different SQL operations such as SELECT, FROM, WHERE, etc. The interpreter evaluates these trees to execute the queries against a database.
This block of code demonstrates the use of the Interpreter Pattern to generate a simple SQL query such as SELECT Name FROM Employees.
C# Code Example
using System;
// Abstract SQL Expression
public interface ISqlExpression
{
string Interpret();
}
// Terminal Expression for SELECT
public class Select : ISqlExpression
{
private string _column;
public Select(string column)
{
_column = column;
}
public string Interpret()
{
return $"SELECT {_column}";
}
}
// Terminal Expression for FROM
public class From : ISqlExpression
{
private string _table;
public From(string table)
{
_table = table;
}
public string Interpret()
{
return $"FROM {_table}";
}
}
// Non-Terminal Expression for SQL Query
public class SqlQuery : ISqlExpression
{
private readonly ISqlExpression _select;
private readonly ISqlExpression _from;
public SqlQuery(ISqlExpression select, ISqlExpression from)
{
_select = select;
_from = from;
}
public string Interpret()
{
return $"{_select.Interpret()} {_from.Interpret()}";
}
}
class SqlProgram
{
static void Main(string[] args)
{
// Creating SQL Query: SELECT Name FROM Employees
ISqlExpression select = new Select("Name");
ISqlExpression from = new From("Employees");
ISqlExpression query = new SqlQuery(select, from);
// Interpreting the SQL query
Console.WriteLine(query.Interpret()); // Outputs: SELECT Name FROM Employees
}
}
Output