设计模式16:Interpreter Pattern(解释者模式)
来源:互联网 发布:软件项目开发计划 编辑:程序博客网 时间:2024/05/22 00:13
本文翻译自:http://www.dofactory.com/Patterns/PatternInterpreter.aspx
Define:Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
一、解释者模式概念
针对某一编程语言,为其语法定义一定的描述语言和解释器,解释器利用描述语言去解释该编程语言中的所有语法。
二、UML类图
三、参与者
该模式的类/对象参与者包括:
- 为执行operation()声明一个接口
- 在语法中使用终止符实现一个operation解释器
- 在语句中要求所有终止符需要一个实例
- one such class is required for every rule R ::= R1R2...Rn in the grammar
- maintains instance variables of type AbstractExpression for each of the symbols R1 through Rn.
- implements an Interpret operation for nonterminal symbols in the grammar. Interpret typically calls itself recursively on the variables representing R1 through Rn.
- 所有全局解释器的内容信息
- builds (or is given) an abstract syntax tree representing a particular sentence in the language that the grammar defines. The abstract syntax tree is assembled from instances of the NonterminalExpression and TerminalExpression classes
- invokes the Interpret operation
四、示例代码
以下结构性的代码描述解释器模式,即利用一个定义好的语法,为进程解析声明提供解释器。
另外一个实例即利用解释器模式来实现如何将罗马数字跟十进制的转换,代码如下:
- 设计模式16:Interpreter Pattern(解释者模式)
- 设计模式 - Interpreter Pattern(解释器模式)
- 设计模式 - Interpreter Pattern(解释器模式)
- 设计模式【解释器模式Interpreter Pattern】
- 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)
- 设计模式读书笔记之解释器模式(Interpreter pattern)
- 设计模式之解释器模式(Interpreter Pattern)
- 设计模式笔记20:解释器模式(Interpreter Pattern)
- 浅谈设计模式:解释器模式(Interpreter Pattern)
- 设计模式(行为型)之解释器模式(Interpreter Pattern)
- 【Java设计模式】· 解释器模式(Interpreter Pattern)
- 设计模式读书笔记之解释器模式(Interpreter pattern)
- 设计模式总结之Interpreter Pattern(解释器模式)
- 设计模式之解释器模式--- Pattern Interpreter
- Java设计模式--解释器模式【Interpreter Pattern】
- 解释器模式(Interpreter Pattern)
- 解释器模式(Interpreter Pattern)
- 解释器模式 Interpreter Pattern
- flex按部就班学习第二节
- 世界上最远的距离
- 她哭了
- 培养创造性思维的20个技巧!
- PHP建站不再难 国外优秀开源PHP建站程序一览
- 设计模式16:Interpreter Pattern(解释者模式)
- 怎样处理测试中不可重现的错误(测试中的UFO)
- 基于C#的通信协议封包(附代码)
- 垂直栅格与渐进式行距(下)
- 实用PHP脚本:最新县及县以上行政区划代码
- Red Hat Enterprise Linux AS 4.7 下 VMware Workstation 6.5.2 build-156735 运行出错,解决方法
- Linux杂记
- 用java调用oracle存储过程总结
- 聊聊CHM工具