Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Interpreter Pattern

Interpreter Pattern

The Interpreter Pattern (Interpreter Pattern) provides a way to evaluate the grammar or expressions of a language; it belongs to the behavioral pattern category.

Given a language, the Interpreter Pattern defines a representation of its grammar and defines an interpreter that uses the representation to interpret sentences in the language.

This pattern is used in SQL parsing, symbol processing engines, etc.

Introduction

Intent

Define a grammar representation for a language and create an interpreter that can interpret sentences in that language..

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

Disadvantages

  1. Limited use cases: Only suitable for simple grammars appropriate for interpretation.
  2. Difficult to maintain: For complex grammars, maintenance and extension become difficult.
  3. Class explosion: May produce many classes, with one class for each grammar rule.
  4. Recursive call: The Interpreter Pattern usually uses recursive calls, which can be difficult to understand and trace.

Usage recommendation

Notes

structures

The Interpreter Pattern contains the following main roles:

Create a class that implements

We will create an interface Expression and a concrete class that implements the Expression The entity class of the interface. Define the class that serves as the main interpreter in the context TerminalExpression class. Other classes OrExpression、AndExpression Used to create composite expressions.

InterpreterPatternDemo, and our demo class uses the Expression Class to create rules and demonstrate expression parsing.

Step 1

Create an expression interface.

Expression.java

public

interface

Expression

{

public

boolean

interpret

(

String

context

)

;

}

Step 2

Create the concrete class that implements the interface above.

TerminalExpression.java

public

class

TerminalExpression

implements

Expression

{

private

String

data

;

public

TerminalExpression

(

String

data

)

{

this

.

data

=

data

;

}

@

Override

public

boolean

interpret

(

String

context

)

{

if

(

context

.

contains

(

data

)

)

{

return

true

;

}

return

false

;

}

}

OrExpression.java

public

class

OrExpression

implements

Expression

{

private

Expression

expr1

=

null

;

private

Expression

expr2

=

null

;

public

OrExpression

(

Expression

expr1

,

Expression

expr2

)

{

this

.

expr1

=

expr1

;

this

.

expr2

=

expr2

;

}

@

Override

public

boolean

interpret

(

String

context

)

{

return

expr1

.

interpret

(

context

)

||

expr2

.

interpret

(

context

)

;

}

}

AndExpression.java

public

class

AndExpression

implements

Expression

{

private

Expression

expr1

=

null

;

private

Expression

expr2

=

null

;

public

AndExpression

(

Expression

expr1

,

Expression

expr2

)

{

this

.

expr1

=

expr1

;

this

.

expr2

=

expr2

;

}

@

Override

public

boolean

interpret

(

String

context

)

{

return

expr1

.

interpret

(

context

)

&&

expr2

.

interpret

(

context

)

;

}

}

Step 3

InterpreterPatternDemo Use Expression Class to create rules and parse them.

InterpreterPatternDemo.java

public

class

InterpreterPatternDemo

{

//

Rule: Robert and John are males

public

static

Expression

getMaleExpression

(

)

{

Expression

robert

=

new

TerminalExpression

(

"

Robert

"

)

;

Expression

john

=

new

TerminalExpression

(

"

John

"

)

;

return

new

OrExpression

(

robert

,

john

)

;

}

//

Rule: Julie is a married woman

public

static

Expression

getMarriedWomanExpression

(

)

{

Expression

julie

=

new

TerminalExpression

(

"

Julie

"

)

;

Expression

married

=

new

TerminalExpression

(

"

Married

"

)

;

return

new

AndExpression

(

julie

,

married

)

;

}

public

static

void

main

(

String

[

]

args

)

{

Expression

isMale

=

getMaleExpression

(

)

;

Expression

isMarriedWoman

=

getMarriedWomanExpression

(

)

;

System

.

out

.

println

(

"

John is male?

"

isMale

.

interpret

(

"

John

"

)

)

;

System

.

out

.

println

(

"

Julie is a married women?

"

isMarriedWoman

.

interpret

(

"

Married Julie

"

)

)

;

}

}

Step 4

Run the program, output result:



John is male? true

Julie is a married women? true