PHP Object-oriented Advanced design pattern: Interpreter Mode Usage instance

Source: Internet
Author: User
What is the interpreter mode?

The interpreter design pattern is used to analyze the key elements of an entity and provide its own interpretations or corresponding actions for each element.


Interpreter mode problems and solutions

The interpreter design pattern is one of the few common design patterns that we often use but are unaware of. This design pattern is not limited to the creation of classes. In the process of creating most programming algorithms, we will use the basic concepts of the interpreter design pattern.

In order to understand how the interpreter design pattern works, we take the example of macro language processing. The command that is written for each macro is itself a collection of more commands. The simplified macro language makes it easier for programmers to complete creation without having to consider the syntax of other system commands. In some cases, this can also improve security, when programmers do not directly access system commands. Instead, programmers write wrapper methods within a sandbox to execute system commands. The red language is interpreted and converted into a set of commands to be executed.

You can also recognize the interpreter design pattern by looking at the template system. A specific predefined keyword or symbol is defined as representing something else. The template processor is used to accept code, interpret each keyword to refer to a specific set of instructions, and execute the code.

Building a system based on the interpreter design pattern allows a third party or user to more flexibly represent and retrieve the data provided by the system. Instead of using predefined method names or specific constants to represent the type of data retrieved, we use the keyword to retrieve the data.


Uml

This diagram details a class design that uses the interpreter design pattern.

Here are the explanations for the explanation:

The 1.MyObject class handles the content that needs to be interpreted. It has a private string content, which stores the contents that need to be processed.

The 2.storeContent () method takes a parameter called content, which pre-interprets the processing and then stores the result inside the MyObject object.

3. The Applyinterpretation () method is then called. This method creates an instance of the Myinterpreter class. The Myinterpreter has a public method named Interpretkeys () that takes the parameter content.applyinterpretation () and obtains the internal contents of the parameter. The Myinterpreter class performs an interpretation of the content to be processed and returns the result to MyObject. Next, the Applyinterpretation () method replaces the internal content variable.

4. Finally, MyObject provides the explanatory content through the GetContent () method.

Working with instances

The code part of the grammatical recursion needs to be implemented according to the specific situation, so it is not reflected in the code. Abstract expressions are the key to generating a collection of grammars, and each non-terminator expression interprets a minimal unit of syntax and then recursively combines the grammatical units into a complete grammar, which is the interpreter pattern.

class Context {}abstract class Expression {public abstract Object interpreter ( Context ctx);} Class Terminalexpression extends Expression {public Object interpreter (Context ctx) {return null;}} Class Nonterminalexpression extends Expression {public nonterminalexpression (expression...expressions) {}public Object Interpreter (Context ctx) {return null;}} public class Client {public static void main (string[] args) {String expression = ""; char[] Chararray = Expression.tochararr Ay (); Context CTX = new context (); stack<expression> stack = new stack<expression> (); for (int i=0;i<chararray.length;i++) {//Syntax judgment, recursive call} Expression exp = Stack.pop (); Exp.interpreter (CTX);}} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.