Interpreter mode (behavior type)
Function
Supports instruction interpretation of languages or symbols that are precisely defined in some syntax.
The Interpreter pattern supports the interpretation of instructions written in a language or notation defined for a specific purpose. The notation is precise and can be defined in terms of a grammar.
Design
Client, the structure of the object, representing the commands of a specific syntax
A class that builds an object structure that represents a set of instructions INS in the given grammar
Context, including the information (input or output) required by the interpreter)
A class that contains information for use by the Interpreter (usually its input and output)
Term, an abstract expression class, which provides the default operations of the interpreter and interfaces of all classes.
An abstract class that provides an interface for all the classes in the structure and a default for the Interpreter operation
Nonterminal, non-terminator expression, can contain other interpreter expressions
A class that implements the Interpreter and also can contain other Term instances
Terminal, termination expression, implementation class of interpreter
A class that implements the Interpreter
Example
Context, description rules and image output
Terminal, a feature such as Top
Nontermial, label such as TextBox
Interpreter: displays the graphic interface after XML Processing
Implementation
Using System;
Using System. Collections;
Class MainApp
{
Static void Main ()
{
Context context = new Context ();
// Usually a tree
ArrayList list = new ArrayList ();
// Populate 'abstract syntax Tree'
List. Add (new TerminalExpression ());
List. Add (new NonterminalExpression ());
List. Add (new TerminalExpression ());
List. Add (new TerminalExpression ());
// Interpret
Foreach (effecactexpression exp in list)
{
Exp. Interpret (context );
}
// Wait for user
Console. Read ();
}
}
// "Context"
Class Context
{
}
// "Effecactexpression"
Abstract class abstractexpression
{
Public abstract void interpret (context );
}
// "Terminalexpression"
Class terminalexpression: abstractexpression
{
Public override void interpret (context)
{
Console. writeline ("called terminal. interpret ()");
}
}
// "Nonterminalexpression"
Class nonterminalexpression: abstractexpression
{
Public override void Interpret (Context context)
{
Console. WriteLine ("Called Nonterminal. Interpret ()");
}
}
Application scenarios
When you have a set of syntax to explain
1. Concise syntax
2. Inefficient
3. Existing analysis syntax tools
4. xml is a type of syntax specification.
Use the Interpreter pattern when...
You have a grammar to be interpreted and:
• The grammar is not too large.
• Efficiency is not critical.
• Parsing tools are available.
• XML is an option for the specification.
Summary
In the process of building software, if the problem in a specific field is complicated and similar models are repeated, it will face frequent changes if you use common programming methods.
In this case, the problem in a specific field is expressed as an expression under a certain syntax rule, and then an interpreter is constructed to explain the expression, so as to solve the problem.
Definition in Design Pattern GoF: defines the syntax of the expression and the interpreter, which uses its syntax to explain the meaning of the expression.
Key Points of Interpreter mode:
Application Scenario: "business rules frequently change, and similar patterns are repeatedly appearing, rules are easy to abstract idioms and rules"
With syntax rules, you can use object-oriented techniques to easily extend the syntax.
The Interpreter mode is suitable for simple syntaxes. For complex syntaxes, the Interpreter mode requires complex class structures and requires a syntax analysis generator.