Interpreter Design Pattern

Interpreter Design Pattern

18 Jun 2024
Intermediate
28.5K Views
2 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Interpreter Design pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc. In this article, I would like to share what is interpreter pattern and how is it work?

What is the Interpreter Design pattern?

 Interpreter Design pattern evaluates/ interprets the instructions written in a language grammar or notations. This pattern involves implementing an expression interface which tells to interpret a particular context. This pattern is used in the compilers or parsers or Macro expansions.

This pattern is commonly used in the menu systems of many applications such as Editor, IDE etc.

Interprets Design Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Interprets design pattern is given below:

interpreter design pattern c#

The classes, interfaces, and objects in the above UML class diagram are as follows:

  1. Client

    This is the class that builds the abstract syntax tree for a set of instructions in the given grammar. This tree builds with the help of instances of NonTerminalExpression and TerminalExpression classes.

  2. Context

    This is a class that contains information (input and output), which is used by the Interpreter.

  3. Expression

    This is an interface that defines the Interpret operation, which must be implemented by each subclass.

  4. NonTerminal

    This is a class that implements the Expression. This can have other instances of Expression.

  5. Terminal

    This is a class that implements the Expression.

C# - Implementation Code

public class Client
{
 public void BuildAndInterpretCommands()
 {
 Context context = new Context("Dot Net context");
 NonterminalExpression root = new NonterminalExpression();
 root.Expression1 = new TerminalExpression();
 root.Expression2 = new TerminalExpression();
 root.Interpret(context);
 }
}

public class Context
{
 public string Name { get; set; }

 public Context(string name)
 {
 Name = name;
 }
}

public interface IExpression
{
 void Interpret(Context context);
}

public class TerminalExpression : IExpression
{
 public void Interpret(Context context)
 {
 Console.WriteLine("Terminal for {0}.", context.Name);
 }
}

public class NonterminalExpression : IExpression
{
 public IExpression Expression1 { get; set; }

 public IExpression Expression2 { get; set; }

 public void Interpret(Context context)
 {
 Console.WriteLine("Nonterminal for {0}.", context.Name);
 Expression1.Interpret(context);
 Expression2.Interpret(context);
 }
}

When to use it?

  1. Need to interpret a grammar that can be represented as large syntax trees.

  2. Parsing tools are available.

  3. Efficiency is not a concern.

Read More Articles Related to Design patterns
What do you think?

I hope you will enjoy the Interpreter Design pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this