当前位置: 代码迷 >> 综合 >> 设计模式16:Interpreter Pattern(解释者模式)
  详细解决方案

设计模式16:Interpreter Pattern(解释者模式)

热度:19   发布时间:2023-12-15 16:26:23.0

本文翻译自: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类图

 

三、参与者

该模式的类/对象参与者包括:

  • AbstractExpression  (描述)
    • 为执行operation()声明一个接口
  • TerminalExpression  ( ThousandExpression, HundredExpression, TenExpression, OneExpression )
    • 在语法中使用终止符实现一个operation解释器
    • 在语句中要求所有终止符需要一个实例
  • NonterminalExpression  ( 不使用 )
    • 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.
  • Context  (Context)
    • 所有全局解释器的内容信息
  • Client  (InterpreterApp)
    • builds (or is given) an abstract syntax tree representing a particular sent
  相关解决方案