设计模式总篇-解释器模式
作者:互联网
/*
@作者:莫忘输赢
@时间:
2020/02/23 6:43
@版本:v1
@解释器模式:
@实现:
实现一个小型解释器所用模式
*/
#include<iostream>
#include<vector>
#include<string>
//using namespace std;
//#include <vld.h>
class Context;
class AbstractExpression
{
public:
virtual void Interpret(Context *context) = 0;
};
class Expression : public AbstractExpression
{
virtual void Interpret(Context *context)
{
std::cout << "终端解释器" << std::endl;
}
};
class NonterminalExpression : public AbstractExpression
{
virtual void Interpret(Context *context)
{
std::cout << "非终端解释器" << std::endl;
}
};
class Context
{
public:
std::string input;
std::string output;
};
int main()
{
Context* context = new Context();
std::vector<AbstractExpression*> expressList;
Expression *expression= new Expression();
expressList.push_back(expression);
NonterminalExpression *nonterminalExpression = new NonterminalExpression();
expressList.push_back(nonterminalExpression);
std::vector<AbstractExpression*> ::iterator p = expressList.begin();
while (p != expressList.end())
{
(*p)->Interpret(context);
p++;
}
delete nonterminalExpression;
delete expression;
delete context;
expressList.clear();
return 0;
}
莫忘输赢 发布了141 篇原创文章 · 获赞 1 · 访问量 5375 私信 关注
标签:解释器,设计模式,Context,总篇,Interpret,context,include,nonterminalExpression,expressList 来源: https://blog.csdn.net/wjl18270365476/article/details/104454386