6.18. Interpreter mode

发布时间 : 2025-10-25 13:34:20 UTC      

Page Views: 9 views

The interpreter pattern (Interpreter Pattern), which is a behavioral pattern, provides a way to evaluate the syntax or expressions of a language. This pattern implements an expression interface that interprets a particular context. This pattern is used in SQL parsing, symbol processing engines, and so on.

6.18.1. Introduction

意图: Given a language, define its grammatical representation and define an interpreter that uses the identity to interpret sentences in the language.

主要解决: Build an interpreter to interpret sentences for some fixed grammars.

何时使用: If a particular type of problem occurs frequently enough, it may be worthwhile to express each instance of the problem as a sentence in a simple language. This allows you to build an interpreter that solves the problem by interpreting these sentences.

如何解决: Construct the grammar tree and define the Terminator and non-Terminator.

关键代码: Build the environment class, which contains some global information outside the interpreter, usually HashMap.

应用实例: Compiler, operation expression evaluation.

优点: 1. Good expansibility and flexibility. 2. A new way to interpret the expression has been added. 3. It is easy to implement simple grammar.

缺点: 1. There are few available scenarios. 2. It is difficult to maintain complex grammar. 3. The interpreter pattern can cause class inflation. 4. The interpreter mode adopts the method of recursive call.

使用场景: 1. A sentence in a language that needs interpretation and execution can be represented as an abstract syntax tree. 2. Some recurrent problems can be expressed in a simple language. 3. A scenario that needs to be explained in a simple grammar.

注意事项: There are few available scenarios. If you encounter it in JAVA, you can use expression4J instead.

6.18.2. Realize

We will create an interface Expression And realized Expression The entity class of the interface. Definition as the main interpreter in the context TerminalExpression Class. Other classes OrExpression AndExpression Used to create composite expressions.

InterpreterPatternDemo Our demo class uses the Expression Class to create rules and demonstrate the resolution of expressions.

解释器模式的 UML 图

6.18.3. Step 1

Create an expression interface.

Expression.java

publicinterfaceExpression{publicbooleaninterpret(Stringcontext);} 

6.18.4. Step 2

Create an entity class that implements the above interface.

TerminalExpression.java

publicclassTerminalExpressionimplementsExpression{privateStringdata;publicTerminalExpression(Stringdata){this.data=data;}@Overridepublicbooleaninterpret(Stringcontext){if(context.contains(data)){returntrue;}returnfalse;}} 

OrExpression.java

publicclassOrExpressionimplementsExpression{privateExpressionexpr1=null;privateExpressionexpr2=null;publicOrExpression(Expressionexpr1,Expressionexpr2){this.expr1=expr1;this.expr2=expr2;}@Overridepublicbooleaninterpret(Stringcontext){returnexpr1.interpret(context)\|\|expr2.interpret(context);}} 

AndExpression.java

publicclassAndExpressionimplementsExpression{privateExpressionexpr1=null;privateExpressionexpr2=null;publicAndExpression(Expressionexpr1,Expressionexpr2){this.expr1=expr1;this.expr2=expr2;}@Overridepublicbooleaninterpret(Stringcontext){returnexpr1.interpret(context)&&expr2.interpret(context);}} 

6.18.5. Step 3

InterpreterPatternDemo Use Expression Class to create rules and parse them.

InterpreterPatternDemo.java

publicclassInterpreterPatternDemo{//规则:Robert 和 John 是男性publicstaticExpressiongetMaleExpression(){Expressionrobert=newTerminalExpression("Robert");Expressionjohn=newTerminalExpression("John");returnnewOrExpression(robert,john);}//规则:Julie 是一个已婚的女性publicstaticExpressiongetMarriedWomanExpression(){Expressionjulie=newTerminalExpression("Julie");Expressionmarried=newTerminalExpression("Married");returnnewAndExpression(julie,married);}publicstaticvoidmain(String[]args){ExpressionisMale=getMaleExpression();ExpressionisMarriedWoman=getMarriedWomanExpression();System.out.println("John is male?"+isMale.interpret("John"));System.out.println("Julie is a married women?"+isMarriedWoman.interpret("Married Julie"));}} 

6.18.6. Step 4

Execute the program and output the result:

John is male? true Julie is a married women? true 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。