解释器模式Interpreter Pattern
定义:一些应用提供了内建(Build-In)的脚本或者宏语言来让用户可以定义他们能够在系统中进行的操作。Interpreter模式的目的就是使用一个解释器为用户提供一个一门定义语言的语法表示的解释器,然后通过这个解释器来解释语言中的句子。
解释器模式类结构图:
解释器模式角色:
Context:解释器上下文环境类。用来存储解释器上下文环境,比如需要解释的文法等。
AbstractExpression:解释器抽象类
ConcreteExpression:解释器具体实现类。
兑现代码:
#include <iostream>
using namespace std;
//PlusExpression MinusExpression
class Context
{
public:
Context(int num)
{
this->m_num = num;
}
int getNum()
{
return m_num;
}
int getRes()
{
return m_res;
}
void setNum(int num)
{
this->m_num = num;
}
void setRes(int res)
{
this->m_res = res;
}
private:
int m_num;
int m_res;
};
class Expression
{
public:
virtual void interpreter(Context*context)=0;
private:
Context *m_context;
};
class PlusExpression:public Expression
{
public:
PlusExpression()
{
this->m_context=NULL;
}
virtual void interpreter(Context*context)
{
int num = context->getNum();
num++;
context->setNum(num);
context->setRes(num);
}
private:
Context* m_context;
};
class MinusExpression:public Expression
{
public:
MinusExpression()
{
this->m_context=NULL;
}
virtual void interpreter(Context*context)
{
int num = context->getNum();
num--;
context->setNum(num);
context->setRes(num);
}
private:
Context* m_context;
};
int main()
{
Expression* expression = NULL;
Context *context = NULL;
Expression* expression2 = NULL;
context = new Context(10);
cout<<context->getNum()<<endl;
expression = new PlusExpression();
expression->interpreter(context);
cout<<context->getRes()<<endl;
context = new Context(10);
cout<<context->getNum()<<endl;
expression2 = new MinusExpression();
expression2->interpreter(context);
cout<<context->getRes()<<endl;
getchar();
return 0;
}