Please enable Javascript to correctly display the contents on Dot Net Tricks!

Interpreter Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 23 Feb 2017
Total Views : 3,003   
Updated On : 24 Feb 2017
 

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 share what is interpreter pattern and how is it work?

What is Interpreter Design pattern?

This 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 a large syntax trees.

  2. Parsing tools are available.

  3. Efficiency is not a concern.

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.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS (0)
6 JUL
ASP.NET Core with Angular4 Development (Online)

7:00 AM-9:00AM IST(+5:30 GMT)T/T

Know More
5 JUL
NodeJS with Angular4 Development (Online)

08:00 PM-09:30 PM IST(+5.30 GMT)M/W/F

Know More
1 JUL
ASP.NET Core with Angular4 Development (Classroom)

3:00PM 5:00PM Weekend

Know More
1 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM 5:00 PM Weekend

Know More
30 JUN
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5.30 GMT) MON/WED/FRI

Know More
24 JUN
ASP.NET MVC with Angular4 Development (Classroom)

8:00 AM-10:00 PM Weekend

Know More
24 JUN
ASP.NET MVC with Angular4 Development (Online)

03:30 PM-05:30 PM IST(+5.30 GMT) Weekend

Know More
24 JUN
NodeJS with Angular4 Development (Classroom)

10:00 AM-12:00 PM Weekend

Know More
23 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

9:30PM- 11:00PM IST(+5:30GMT) M/W/F

Know More
23 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

9:30PM- 11:00PM IST(+5:30GMT) M/W/F

20 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST(+5:30 GMT)T/W/T

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)(TT)

3 JUN
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:30 PM-7:00 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+