The Interpreter (Interpreter) mode, which includes a syntactic representation with a composite class layered structure, the rule is mapped to the class, the expressions following the syntax can be converted into an abstract syntax tree. Except for the instance object graph in the composite mode, there is no other content.
Tree is an abstract term, because in most cases it is an abstract expression, which ignores the possibility of a string or a specific expression of the data structure (for example, in PHP, "A" and "\ x41" are different manifestations of the same abstract literal value.) the interpretation process is greatly simplified through logical rule decoupling.
The interpreter is not a common mode, but for simple syntax, it is as easy to add a rule as adding a class, however, it does not solve the transformation from the specific expression form to the abstract syntax tree, which is completed by other services.
The interpreter mode is designed to implement a simple abstract expression (Interpreter operation) using a composite layered structure. Parameters of interpreter operations are generally referred to as context, for a given method, they are usually replaced by calculated values, or they may not exist for some operations.
Similarly, when a interpreter is included, the leaf and container participant names in composite mode are different. These names reflect the roles they play: terminal or nonterminal) expression.
Participants:
◆ Client: explains the operation.
◆ AbstractExpression: abstraction based on an expression tree.
◆ NonTerminalExpression: A non-terminalexpression expression that recursively contains other abstract expressions.
◆ TerminalExpression: an expression that cannot be further simplified.
The design pattern book provides an extension example for this pattern. I will use a mathematical expression to replace the Boolean expression and reengineer it. Therefore, this example solves the display of a mathematical expression, its evaluate () is separated in a different ConcreteExpression class.
Copy codeThe Code is as follows:
/**
* AbstractExpression. All implementations of this interface
* Are ConcreteExpressions.
*/
Interface MathExpression
{
/**
* Calculates the value assumed by the expression.
* Note that $ values is passed to all expression but it
* Is used by Variable only. This is required to abstract
* Away the tree structure.
*/
Public function evaluate (array $ values );
}
/**
* A terminal expression which is a literal value.
*/
Class Literal implements MathExpression
{
Private $ _ value;
Public function _ construct ($ value)
{
$ This-> _ value = $ value;
}
Public function evaluate (array $ values)
{
Return $ this-> _ value;
}
}
/**
* A terminal expression which represents a variable.
*/
Class Variable implements MathExpression
{
Private $ _ letter;
Public function _ construct ($ letter)
{
$ This-> _ letter = $ letter;
}
Public function evaluate (array $ values)
{
Return $ values [$ this-> _ letter];
}
}
/**
* Nonterminal expression.
*/
Class Sum implements MathExpression
{
Private $ _;
Private $ _ B;
Public function _ construct (MathExpression $ a, MathExpression $ B)
{
$ This-> _ a = $;
$ This-> _ B = $ B;
}
Public function evaluate (array $ values)
{
Return $ this-> _ a-> evaluate ($ values) + $ this-> _ B-> evaluate ($ values );
}
}
/**
* Nonterminal expression.
*/
Class Product implements MathExpression
{
Private $ _;
Private $ _ B;
Public function _ construct (MathExpression $ a, MathExpression $ B)
{
$ This-> _ a = $;
$ This-> _ B = $ B;
}
Public function evaluate (array $ values)
{
Return $ this-> _ a-> evaluate ($ values) * $ this-> _ B-> evaluate ($ values );
}
}
// 10 (a + 3)
$ Expression = new Product (new Literal (10), new Sum (new Variable ('A'), new Literal (3 )));
Echo $ expression-> evaluate (array ('A' => 4), "\ n ";
// Adding new rules to the grammar is easy:
// E.g. Power, Subtraction...
// Thanks to the Composite, manipulation is even simpler:
// We cocould add substitute ($ letter, MathExpression $ expr)
// To the interface...