This description of the interpreter (interpreter) pattern is described in Dr Shanhong's "Java and schema" book:
The interpreter pattern is the behavior pattern of the class. Given a language, the interpreter pattern can define a representation of its grammar and provide an interpreter at the same time. The client can use this interpreter to interpret the sentences in the language.
Structure of the Interpreter pattern
The following is an example of a schematic system that discusses the structure of the interpreter pattern. The structure of the system is shown as follows:
The roles involved in the pattern are as follows:
(1) abstract expression (Expression) role: declares an abstract interface that all specific expression roles need to implement. This interface is primarily a interpret () method, called an interpretation operation.
(2) non-terminal expression (Terminal Expression) Role: Implements the interface required by the abstract expression role, primarily a interpret () method; Each non-terminal in the grammar has a specific ending expression corresponding to it. For example, there is a simple formula R=R1+R2, in which R1 and R2 are non-terminal, the corresponding parsing R1 and R2 interpreter is the non-terminal expression.
(3) Non-non-terminal expression (nonterminal Expression) role: Every rule in a grammar requires a specific non-terminal expression, and a non-non-terminal expression is generally an operator or other keyword in the grammar, such as the formula R=R1+R2, "+" is not non-terminal, The interpreter that parses "+" is a non-terminal expression.
(4) environment (context) role: The role of the task is generally used to store the grammar of each non-terminal corresponding to the specific values, such as R=R1+R2, we give R1 value 100, to R2 assigned 200. This information needs to be stored in the environment role, and in many cases it is sufficient that we use the map to act as an environmental role.
To illustrate the implementation of the interpreter pattern, here is an implementation of the simplest grammar and corresponding interpreter pattern, which is the operation and evaluation of Boolean expressions in the simulation Java language.
In this language, Non-terminal is a Boolean variable, which is constant true and false. A non-terminal expression contains Boolean expressions such as operator and,or and not. This simple grammar is as follows:
Copy Code code as follows:
Expression:: = Constant | Variable | Or | and | Not
And:: = Expression ' and ' Expression
Or:: = Expression ' or ' Expression
Not:: = ' not ' Expression
Variable:: = any identifier
Constant:: = ' true ' | ' False '
The structure diagram of the interpreter pattern is as follows:
Source
An abstract expression role
Copy Code code as follows:
Public abstract class Expression {
/**
* In the context of the environment, this method interprets any given expression
*/
Public abstract Boolean interpret (context CTX);
/**
* Verify that the two expressions are structurally identical
*/
Public abstract Boolean equals (Object obj);
/**
* Returns the hash code of an expression
*/
public abstract int hashcode ();
/**
* Convert an expression to a string
*/
Public abstract String toString ();
}
A constant object represents a Boolean constant
Copy Code code as follows:
public class Constant extends expression{
private Boolean value;
Public Constant (Boolean value) {
This.value = value;
}
@Override
public boolean equals (Object obj) {
if (obj!= null && obj instanceof Constant) {
return this.value = = ((Constant) obj). value;
}
return false;
}
@Override
public int hashcode () {
Return this.tostring (). Hashcode ();
}
@Override
Public Boolean interpret (context CTx) {
return value;
}
@Override
Public String toString () {
return new Boolean (value). toString ();
}
}
A variable object represents a named variable
Copy Code code as follows:
public class Variable extends Expression {
private String name;
public Variable (String name) {
this.name = name;
}
@Override
public boolean equals (Object obj) {
& nbsp;
if (obj!= null && obj instanceof Variable)
{
return this.name.equals (
((Variable) obj). Name);
}
return false;
}
@Override
public int hashcode () {
Return this.tostring (). Hashcode ();
}
@Override
Public String toString () {
return name;
}
@Override
Public Boolean interpret (context CTx) {
Return Ctx.lookup (this);
}
}
Represents the and class of the logical and action, which represents the operation of a new Boolean expression by a logical "and" operation of two Boolean expressions
Copy Code code as follows:
public class and extends Expression {
private Expression left,right;
public and (Expression left, Expression right) {
& nbsp; This.left = left;
this.right = right;
}
@Override
public boolean equals (Object obj) {
if (obj!= null && obj instanceof and)
{
return Left.equals ((and) obj. Left) &
Right.equals ((and) obj). right);
}
return false;
}
@Override
public int hashcode () {
Return this.tostring (). Hashcode ();
}
@Override
Public Boolean interpret (context CTx) {
Return Left.interpret (CTX) && Right.interpret (CTX);
}
@Override
Public String toString () {
Return "(" + left.tostring () + "and" + right.tostring () + ")";
}
}
An or class that represents a logical or action that represents the operation of a new Boolean expression by a logical "or" operation of two Boolean expressions
Copy Code code as follows:
public class Or extends Expression {
Private Expression left,right;
Public Or (Expression left, Expression right) {
This.left = left;
This.right = right;
}
@Override
public boolean equals (Object obj) {
if (obj!= null && obj instanceof Or)
{
Return This.left.equals ((or) obj. left) && this.right.equals ((or) obj). right);
}
return false;
}
@Override
public int hashcode () {
Return this.tostring (). Hashcode ();
}
@Override
Public Boolean interpret (context CTx) {
Return Left.interpret (CTX) | | Right.interpret (CTX);
}
@Override
Public String toString () {
Return "(" + left.tostring () + "OR" + right.tostring () + ")";
}
}
A not class that represents a logical "non" operation that represents an operation that gives a new Boolean expression by a Boolean expression through a logical "non" operation
Copy Code code as follows:
public class not extends Expression {
private Expression exp;
public Not (Expression exp) {
& nbsp THIS.EXP = exp;
}
@Override
public boolean equals (Object obj) {
if (obj!= null && obj instanceof not)
{
return exp.equals (
(not) obj. exp );
}
return false;
}
@Override
public int hashcode () {
Return this.tostring (). Hashcode ();
}
@Override
Public Boolean interpret (context CTx) {
Return!exp.interpret (CTX);
}
@Override
Public String toString () {
Return "(not" + exp.tostring () + ")";
}
}
The environment (context) class defines a mapping from a variable to a Boolean value
Copy Code code as follows:
public class Context {
Private map<variable,boolean> Map = new hashmap<variable,boolean> ();
public void assign (Variable var, boolean value) {
Map.put (Var, new Boolean (value));
}
Public Boolean lookup (Variable var) throws illegalargumentexception{
Boolean value = Map.get (Var);
if (value = = null) {
throw new IllegalArgumentException ();
}
return Value.booleanvalue ();
}
}
Client class
Copy Code code as follows:
public class Client {
public static void Main (string[] args) {
context CTX = new context ();
Variable x = new Variable ("X");
Variable y = new Variable ("Y");
Constant C = new Constant (true);
ctx.assign (x, False);
ctx.assign (y, true);
Expression exp = new Or (New and (C,x), new and (Y,new not (x)));
System.out.println ("x=" + X.interpret (CTX));
System.out.println ("y=" + Y.interpret (CTX));
System.out.println (exp.tostring () + "=" + Exp.interpret (CTX));
}
}
The results of the operation are as follows: