Qt C++ interpreter模式 (解释器模式)

这个模式常用于正则表达式等

interpreter.h

#pragma once


#include


class CVariable;


class CVarientContext
{
public:
CVarientContext(){}
virtual ~CVarientContext(){}
virtual void addVarient(CVariable* key,int val) = 0;
virtual void removeVarient(CVariable* key) = 0;
virtual void setVarient(CVariable* key,int val) = 0;
virtual int getVarient(CVariable* key) const = 0;


protected:
QMap m_mapVar;
};


class CDataVarient : public CVarientContext
{
public:
CDataVarient(){}
virtual ~CDataVarient(){}
void addVarient(CVariable* key,int val)
{
m_mapVar.insert(key,val);
}
void removeVarient(CVariable* key)
{
m_mapVar.remove(key);
}
void setVarient(CVariable* key,int val)
{
m_mapVar.insert(key,val);
}
int getVarient(CVariable* key) const
{
return m_mapVar.value(key);
}
};


class CAbstractMathExpress
{
public:
virtual int interpret(CVarientContext *varient) = 0;
};




class CContext : public CAbstractMathExpress
{
public:
CContext(int k):m_k(k){}
virtual ~CContext(){}
int interpret(CVarientContext *varient){return m_k;}
private:
int m_k;
};


class CVariable : public CAbstractMathExpress
{
public:
CVariable(QString name):m_name(name){}
virtual ~CVariable(){}
QString getName() const{return m_name;}


int interpret(CVarientContext *varient);


private:
QString m_name;
};


class CAddExpress : public CAbstractMathExpress
{
public:
CAddExpress(CAbstractMathExpress* left,CAbstractMathExpress* right)
:m_left(left),m_right(right){}


int interpret(CVarientContext *varient);
private:
CAbstractMathExpress *m_left;
CAbstractMathExpress *m_right;


};


class CSubExpress : public CAbstractMathExpress
{
public:
CSubExpress(CAbstractMathExpress* left,CAbstractMathExpress* right)
:m_left(left),m_right(right){}


int interpret(CVarientContext *varient);
private:
CAbstractMathExpress *m_left;
CAbstractMathExpress *m_right;


};


class CMulExpress : public CAbstractMathExpress
{
public:
CMulExpress(CAbstractMathExpress* left,CAbstractMathExpress* right)
:m_left(left),m_right(right){}


int interpret(CVarientContext *varient);
private:
CAbstractMathExpress *m_left;
CAbstractMathExpress *m_right;


};


class CDivExpress : public CAbstractMathExpress
{
public:
CDivExpress(CAbstractMathExpress* left,CAbstractMathExpress* right)
:m_left(left),m_right(right){}


int interpret(CVarientContext *varient);
private:
CAbstractMathExpress *m_left;
CAbstractMathExpress *m_right;


};


interpreter.cpp

#include "interpreter.h"


int CVariable::interpret(CVarientContext *varient)
{
return varient->getVarient(this);
}


int CAddExpress::interpret(CVarientContext *varient)
{
return m_left->interpret(varient) + m_right->interpret(varient);
}


int CSubExpress::interpret(CVarientContext *varient)
{
return m_left->interpret(varient) - m_right->interpret(varient);
}


int CMulExpress::interpret(CVarientContext *varient)
{
return m_left->interpret(varient) * m_right->interpret(varient);
}


int CDivExpress::interpret(CVarientContext *varient)
{
return m_left->interpret(varient) / m_right->interpret(varient);
}



main.cpp

#include
#include "interpreter.h"


int main(int argc,char **argv)
{
QApplication app(argc,argv);

CVarientContext *context = new CDataVarient();

CVariable *a = new CVariable("A");
CVariable *b = new CVariable("B");
context->addVarient(a,20);
context->addVarient(b,10);
CAbstractMathExpress *exp = new CAddExpress(a,b);

int value = exp->interpret(context);


return app.exec();
}

你可能感兴趣的:(C++设计模式)