Interpreter Pattern

August 8, 2008

no comments

Interpreter Pattern

This post is the last post in the series of design patterns.
the post will describe why and how to use the interpreter design pattern and will
have a C# example of how to implement the pattern.
You can read my previous posts about design patterns here:
Structural patterns
Decorator pattern 
Proxy pattern
Facade pattern
Adapter pattern
Composite pattern
Bridge pattern
Flyweight pattern

Creational patterns
Singleton pattern
Abstract Factory pattern
Prototype pattern
Factory Method pattern
Builder pattern

Behavioral Patterns
Strategy pattern
Iterator pattern 
Template method pattern
Command pattern
Chain of responsibility pattern 
Mediator pattern
Memento pattern
State pattern
Visitor pattern
Observer pattern

Interpreter Pattern    
The interpreter design pattern enables the reading and interpreting
of instruction that are written in a specific purpose language or notation.
The interpreter uses the representation of the grammar of the language
in order to interpret the language instructions. 
Using the pattern can cause a performance issue.
 
Use Cases for the Interpreter Pattern

You should use the pattern in the following cases:

  • You have a grammar for a language that isn’t large and complicated.
  • Performance isn’t an issue in the application.
  • You have a way to parse the grammar in an efficient way.

UML Diagram     
Interpreter Pattern       

Example in C#
The following code is an example of how to implement the pattern:

    #region Context

    public class InterpreterContext

    {

    }

    #endregion

    #region Expression

    public abstract class AbstractTerm

    {

        #region Methods

        public abstract void Interpret(InterpreterContext context);

        #endregion

    }

    #endregion

    #region Concrete Expressions

    public class TerminalTerm : AbstractTerm

    {

        #region Methods

        public override void Interpret(InterpreterContext context)

        {

            Console.WriteLine("Terminal");

        }

        #endregion

    }

    public class NonterminalTerm : AbstractTerm

    {

        #region Methods

        public override void Interpret(InterpreterContext context)

        {

            Console.WriteLine("Nonterminal");

        }

        #endregion

    }

    #endregion

The example is very simple and follows the instructions of the UML shown above. 

The following code is an example scenario of how to run the pattern in
console application:

   InterpreterContext context = new InterpreterContext();

   List<AbstractTerm> terms = new List<AbstractTerm>();

   // Build the terms tree

   terms.Add(new TerminalTerm());

   terms.Add(new NonterminalTerm());

   terms.Add(new TerminalTerm());

   // Interpret the tree

   foreach (AbstractTerm term in terms)

   {

      term.Interpret(context);

   }

   Console.Read();

Summary
To sum up the post, I explained the interpreter design pattern in the post.
The pattern is rarely used and implies performance issues in the application.
I hope that you enjoyed the journey in the world of design patterns and that
the posts where helpful.
See you in the next posts in other development subjects.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>