[Behavioral Patterns] Understanding of Interpreter Patterns




head File
//InterpreterPattern.h

#ifndef INTERPRETER_PATTERN_H
#define INTERPRETER_PATTERN_H

#include <Windows.h>
#include <iostream>
#include <map>
#include <string>
using namespace std;

namespace InterpreterPattern
{
    //////////////////////////////////////////////////////////////////////////
    class Context
    {
    private:
        map<string, int> m_cValueMap;

    public:
        void addValue(string key,int value);
        int getValue(string key);
    };

    //////////////////////////////////////////////////////////////////////////
    class AbstractExpression
    {
    public :
        virtual int interpreter(Context context) = 0;
    };

    //////////////////////////////////////////////////////////////////////////
    class AddNonterminalExpression : public AbstractExpression
    {
    private :
        AbstractExpression* m_pLeft;
        AbstractExpression* m_pRight;

    public:
        AddNonterminalExpression(AbstractExpression* pLeft, AbstractExpression* pRight);
        int interpreter (Context cContext);
    };

    //////////////////////////////////////////////////////////////////////////
    class SubtractNonterminalExpression : public AbstractExpression
    {
    private :
        AbstractExpression* m_pLeft;
        AbstractExpression* m_pRight;

    public:
        SubtractNonterminalExpression(AbstractExpression* pLeft, AbstractExpression* pRight);
        int interpreter (Context cContext);
    };

    //////////////////////////////////////////////////////////////////////////
    class TerminalExpression : public AbstractExpression
    {
    private :
        int i;

    public :
        TerminalExpression(int i);
        int interpreter (Context cContext);
    };

    //////////////////////////////////////////////////////////////////////////
    void InterpreterPattern_Test();
}

#endif


accomplish
#include "InterpreterPattern.h"

namespace InterpreterPattern
{
    //////////////////////////////////////////////////////////////////////////
    void Context::addValue(string key,int value)
    {
        m_cValueMap.insert(std::pair<string,int>(key,value));
    }

    int Context::getValue(string key)
    {
        return m_cValueMap[key];
    }

    //////////////////////////////////////////////////////////////////////////
    AddNonterminalExpression::AddNonterminalExpression(AbstractExpression* pLeft, AbstractExpression* pRight)
    {
        this->m_pLeft = pLeft;
        this->m_pRight = pRight;
    }

    int AddNonterminalExpression::interpreter(Context cContext)
    {
        return this->m_pLeft->interpreter(cContext) + this->m_pRight->interpreter(cContext);
    }

    //////////////////////////////////////////////////////////////////////////
    SubtractNonterminalExpression::SubtractNonterminalExpression(AbstractExpression* pLeft, AbstractExpression* pRight)
    {
        this->m_pLeft =  pLeft;
        this->m_pRight = pRight;
    }

    int SubtractNonterminalExpression::interpreter(Context cContext)
    {
        return this->m_pLeft->interpreter(cContext) - this->m_pRight->interpreter(cContext);
    }

    //////////////////////////////////////////////////////////////////////////
    TerminalExpression::TerminalExpression(int i)
    {
        this->i = i;
    }

    int TerminalExpression :: interpreter (Context cContext)
    {
        return this->i;
    }

    //////////////////////////////////////////////////////////////////////////
    void InterpreterPattern_Test()
    {
        //a-b+c
        Context cContext;
        cContext.addValue("a", 7);
        cContext.addValue("b", 8);
        cContext.addValue("c", 2);

        SubtractNonterminalExpression* pSubtractValue = new SubtractNonterminalExpression(new TerminalExpression(
            cContext.getValue("a")), new TerminalExpression(cContext.getValue("b")));

        AddNonterminalExpression* pAddValue = new AddNonterminalExpression(pSubtractValue, new TerminalExpression(
            cContext.getValue("c")));

        cout<< pAddValue->interpreter(cContext);
    }
}


client
#include "InterpreterPattern.h"


#include <iostream>
using namespace std;
using namespace InterpreterPattern;

void main()
{
    InterpreterPattern_Test();
}

operation result

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326821939&siteId=291194637