SIGN UP MEMBER LOGIN:    
ARTICLE

Interpreter Pattern

Posted by Jean Paul Articles | Design & Architecture January 27, 2012
In this article I would like to demonstrate use of the Interpreter pattern.
Reader Level:
Download Files:
 

This is not a widely used pattern but reveals a good way of programming. It says about creating a language along with an interpreter for the language. The language can be used to represent states, actions, loops etc. depending on the programming requirement. Here we are using the pattern to resolve a persistence problem.

Challenge

You are working on a Drawing application which allows creating lines and circles on a canvas. The classes Line and Circle do the job for you. You need to communicate the drawing with multiple users who have their own copy of the application. The current persistence of drawing using serialization of the above classes is creating large sized files and does not allow sending the message over a chat window.

How to provide a better solution for persistence with smaller size and easier communication?

Definition

"Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language"

Implementation

We can use the Interpreter pattern to approach the problem. We need to construct a language to include the commands and an interpreter to execute the actions.

Interpreter1.gif

The following are the classes involved for our drawing application:

  • A Line Class
  • A Circle Class
  • An Interpreter Class

The Line and Circle class includes the drawing functionality. The Interpreter class takes care of converting the commands to invoking of appropriate line or circle classes.

The Language

The language part could be constituted by a series of sentences. The sentence could be as follows:

line,10,10,10,10 Line command stating x, y, width, height
circle,10,10,50 Circle command stating x, y, radius

Please note that the arguments are separated using a comma (,).

The sentence could be separated using a semicolon (;) as shown below.

line,60,60,50,0;line,110,110,50,0;

Executing the Application

We can enter the series of commands separated by a semicolon (;). The commands are entered in the application.

Interpreter2.gif

Clicking Execute you will see the drawings of lines and circles shown above.

The body of the Interpreter Execute() method is given below:

public void Execute(string commands, Graphics graphics)
{
    graphics.Clear(Color.FromArgb(237, 253, 200)); 
    int i = 0;
 
    foreach (string rawCommand in commands.Split(';'))
    {
        string command = rawCommand.Replace(Environment.NewLine, string.Empty).Trim();
 
        if (command.StartsWith("line"))
        {
            i = 0;
            int x = 0, y = 0, width = 0, height = 0; 
            foreach (string argument in command.Split(','))
            {
                if (i == 1)
                    x = int.Parse(argument);
 
                else if (i == 2)
                    y = int.Parse(argument); 
                else if (i == 3)
                    width = int.Parse(argument);
 
                else if (i == 4)
                    height = int.Parse(argument); 
                i++;
            } 
            new Line(graphics).Draw(x, y, width, height);
        }
        else if (command.StartsWith("circle"))
        {
            i = 0;
            int x = 0, y = 0, radius = 0; 
            foreach (string argument in command.Split(','))
            {
                if (i == 1)
                    x = int.Parse(argument); 
                else if (i == 2)
                    y = int.Parse(argument); 
                else if (i == 3)
                    radius = int.Parse(argument); 
                i++;
            } 
            new Circle(graphics).Draw(x, y, radius);
        }
    }
}


Following is the class diagram:

Interpreter3.gif

This concludes our application using the Interpreter Pattern. Now the actions could be converted to a language for interpreting later.

Comparison of Command Pattern and Interpreter Pattern

You can find some similarities between the Command and Interpreter patterns. The command pattern says about converting the actions into command for logging or undo purposes. Here the commands are objects but for the Interpreter pattern the commands are sentences.

The Command pattern can be used for persisting objects by serializing the command list and results in large sized files compared with an Interpreter pattern.

The Interpreter pattern provides an easier approach in the runtime but comes at the cost of developing an interpreter.

Applications of Interpreter Pattern

You are creating your own Photoshop application. The objects drawn on the layer can be converted to a series of commands. Later the entire drawing could be represented using the command file which will be small in size compared to the entire canvas serialized.

You will get the advantage of sending the file to another person where he/she can:

  • Regenerate the Drawing
  • View all the Draw Actions
  • Modify the Draw Action Items
  • Undo the last draw actions

References

http://www.dofactory.com/Patterns/PatternInterpreter.aspx#_self1

Summary

In this article we have seen the Interpreter pattern with a small application using it. The above application shows only a simple usage for learning purpose but in the real-world scenarios the language may include loops, conditions etc. The attachment contains the source code of the application we have discussed.

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You Vineet for the good words. I am happy that it was worth writing. The link provides the 23 Design Patterns as an eBook. http://jeanpaulva.com/2012/01/27/ebook-on-design-patterns/

Posted by Jean Paul Jan 29, 2012

Good article....

Posted by Vineet Kumar Saini Jan 28, 2012

Yes it is a nice pattern. Thank You Arjun for the good words..

Posted by Jean Paul Jan 27, 2012

Thanks Mr.Jean Paul

Posted by Arjun Panwar Jan 27, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor