Interpreter design pattern for beginners.

Interpreter design pattern–

Most of the time, we don’t see this design pattern during our day to day coding practice. Or at office work. The reason why is because we have so many third party libraries to solve pain addressed by this pattern. For example –
  1. You want to validate semantic correctness of mathematical expression.
  2. It is for grammar checker or rule engine.
  3. SQL parsing, symbol parsing, converting high-level language to a low level.
  4. If you want to develop a new computer language to solve X, Y, Z problem, then also you can use it.

As most of the time, we are creating software to address some high-level business requirements. We don’t get much opportunity to develop something like mentioned in the above list. And if you have a core project at your office work, where you do those things. Then stick to it and don’t switch your job.

Many times, composite design pattern implement interpreter within them.


UML structure -

interpreter Dp
UML structure of interpreter design pattern.

Example with C++ code - 

Below mentioned example is a single level of interpretation. In most compiler and expression evaluation related task. We may end up with nested levels of interpretation. As mentioned in the above UML structure, but this is a small code example to demonstrate interpreter. 
interpreter DP ex
Example of interpreter design pattern (Click on picture to see zoom view).



#include<iostream>
#include<string>
#include <sstream>
#include <stdlib.h> // for atoi
#include <stdio.h> // for c - printf function
using namespace std;

class LanguageConverter
{
    public:
        LanguageConverter():m_isForPrint(false),m_isForAdd(false){}
        virtual void getInput(string input) = 0;
    
    protected:
        bool m_isForPrint;
        bool m_isForAdd;
        string m_printMsg;
        int m_array[2];
};
void LanguageConverter::getInput(string input)
{
    size_t pos;
    pos = input.find("hello");
    if(pos != string::npos)
    {
        m_isForPrint = true;
        m_printMsg =  input.substr(pos, (input.length() - pos)) ;
    }
    else
    {
        pos = input.find("add");
        if(pos != string::npos)
        {
            m_isForAdd = true;
            stringstream  tempString(input.substr(pos, (input.length() - pos)));
            string temp;
            int index = 0;
            std::string::size_type sz;
            while (tempString.good())
            {
                tempString >> temp;
                if(temp[0] >= 48 && temp[0] <= 57)
                {
                    m_array[index] = atoi(temp.c_str());
                    index++;
                }
            }
        }
    }
}
class CPPLanguageConverter: public LanguageConverter
{
    public:
        void getInput(string input)
        {
            LanguageConverter::getInput(input);
            cout<< endl<< "Using CPP APIs "<<endl;
            if(m_isForPrint)
            {
                print();
                m_isForPrint = false;
            }
            else if(m_isForAdd)
            {
                add();
                m_isForAdd = false;
            }
            else
            {
                cout<< "wrong input"<< endl;
            }
        }
        
        void add()
        {
            cout << "Sum of " << m_array[0] << " and " << m_array[1] << " is - " << (m_array[0] + m_array[1]);
        }
        
        void print()
        {
            cout<< " " << m_printMsg <<endl;
        }
};

class CLanguageConverter: public LanguageConverter
{
    public:
        void getInput(string input)
        {
            LanguageConverter::getInput(input);
            printf("\n \n Using C APIs");
            if(m_isForPrint)
            {
                print();
                m_isForPrint = false;
            }
            else if(m_isForAdd)
            {
                add();
                m_isForAdd = false;
            }
            else
            {
                cout<< "wrong input"<< endl;
            }
        }
        
        void print()
        {
            // printf is C language's API to print msg on console.
            printf("\n %s", m_printMsg.c_str());
        }
        
        void add()
        {
            printf("\n Sum of %d and %d is - %d ", m_array[0], m_array[1],(m_array[0] + m_array[1]));
        }
    
};

/*
#######################################################################################
#########  __  __       _        ######################################################
######### |  \/  | __ _(_)_ __   ######################################################
######### | |\/| |/ _` | | '_ \  ######################################################
######### | |  | | (_| | | | | | ######################################################
######### |_|  |_|\__,_|_|_| |_| ######################################################
#######################################################################################
*/
int main(int argc, char **argv)
{
    string inputOne = "Hi computer I want to print hello world this is plain English programming language";
    string inputTwo = "Hi computer I want to add two numbers 10 and 20";
    // this will code your requirement to c++
    LanguageConverter *cpp = new CPPLanguageConverter;
    cpp->getInput(inputOne);
    cpp->getInput(inputTwo);
    
    // this will code your requirement to c
    LanguageConverter *c = new CLanguageConverter;
    c->getInput(inputOne);
    c->getInput(inputTwo);
    return 0;
}


///////////////////////////////////////////////////////////////////-------------OutPut
/*
$ ./a.exe

Using CPP APIs
 hello world this is plain English programming language

Using CPP APIs
Sum of 10 and 20 is - 30

 Using C APIs
 hello world this is plain English programming language

 Using C APIs
 Sum of 10 and 20 is - 30

*/



Thanks for reading it. To learn more about design patterns and basic design principles, please see my web page.

Comments

Popular posts from this blog

Non-virtual interface idiom (NVI)

Factory method design pattern for beginners.

Architectural patterns => Mud to structure => layers.