Interpreter(解释器)设计模式
来源:互联网 发布:写作文网络词语 编辑:程序博客网 时间:2024/05/22 01:29
声明:本博文篇幅短,适合review。
一、概念
给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
二、模式结构图
三、例子
class Context {public:string getInput(){return mInput;}void setInput(string input){mInput = input}string getOutput(){return mOutput;}void setOutput(string output){mOutput = output}private:string mInput;string mOutput;};class AbstractExpression{public:virtual void interpret(Context context) = 0;};class TerminalExpression : public AbstractExpression{public:void interpret(Context context){cout<<"TerminalExpression interpret"<<endl;}};class NonTerminalExpression : public AbstractExpression{public:void interpret(Context context){cout<<"NonTerminalExpression interpret"<<endl;}};void main(){Context * context = new Context();Vector<AbstractExpression *> vec;vec.push_back(new NonTerminalExpression());vec.push_back(new NonTerminalExpression());vec.push_back(new TerminalExpression());for(Vector<AbstractExpression *>::iterator it = vec.begin(); it != vec.end(); it++){(*it)->interpret(context);}}应用如:正则表达式、浏览器解析html等
四、缺点
1、优点
a、每一条文法规则都可以表示为一个类,因此可以方便地实现一个简单的语言。
b、易于改变和扩展文法。
2、缺点
a、对于复杂文法难以维护。
1 0
- Interpreter(解释器)设计模式
- 设计模式(20)-解释器模式(Interpreter)
- 设计模式之Interpreter解释器模式
- 设计模式----Interpreter(解释器)模式
- 设计模式 - Interpreter Pattern(解释器模式)
- 设计模式 - Interpreter Pattern(解释器模式)
- 设计模式--解释器模式(Interpreter)
- 设计模式(22)-解释器模式(Interpreter)
- 设计模式15 - Interpreter 解释器模式
- [设计模式]Interpreter解释器模式
- 设计模式之解释器模式,interpreter
- 设计模式(22) - Interpreter解释器模式
- 设计模式之解释器模式(interpreter)
- 行为设计模式---解释器模式(Interpreter)
- 设计模式 笔记 解释器模式 Interpreter
- c++设计模式----解释器模式interpreter
- 设计模式之:解释器模式(Interpreter)
- 设计模式:解释器模式(Interpreter)
- jQuery第一天 hide()函数
- android 一些易混淆的方法1
- 浏览器兼容字符编码
- iOS中OpenGL应用
- JS入门之arguments对象
- Interpreter(解释器)设计模式
- 图基本算法库
- HDOJ 5596 GTW likes gt (线段树区间更新+单点查询+技巧)
- 性能优化模式-美团技术团队
- [BZOJ1688] [Usaco2005 Open]Disease Manangement 疾病管理
- jQuery事件操作链式例子
- VS2015配置OpenMVS流程
- 机器学习:Apriori发现频繁项集和关联规则
- GOOGLE 面试