[Design mode-behavior type] Interpreter mode (Interpreter)

Source: Internet
Author: User

[Design mode-behavior type] Interpreter mode (Interpreter)
One sentence

It seems to be a grammar used to explain a language. (Similar to different interpreter sub-classes that explain different characters)

Similar to the interpreter of the compiler, the actual situation may be less used.


Summary


Analysis

INTERPRETER-I have a "bubble MM Sutra" with various tips on bubble MM, such as the steps to eat western food and the methods to watch movies. When dating with MM, I only need to create an Interpreter, run the script above.
Interpreter mode: Given a language, the interpreter mode can define a representation of its syntax and provide an interpreter at the same time. The client can use this interpreter to explain sentences in this language. The interpreter mode describes how to explain these statements with a simple syntax. The language mentioned in the interpreter mode refers to any combination that any interpreter object can interpret. In the interpreter mode, you need to define a hierarchical structure of a command class that represents grammar, that is, a series of combination rules. Each command object has an interpretation method, which represents an interpretation of the command object. Any permutation and combination of objects in the hierarchical structure of command objects is a language.


Instance

The files include:

Abstract class of an interpreter (Expression. java)

A Context class, which contains a list of interpreter classes (Context. java)

Subclass of the two Interpreters (Type1Expression. java; Type2Expression. java)

Use different interpreters to convert the corresponding language.

Test class TestMain. java

/**    * @author oscar999    * @date 2015-1-6 * @version V1.0    */package designptn.interpreter;public abstract class Expression {abstract void interpret(Context ctx);}

/**    * @author oscar999    * @date 2015-1-6 * @version V1.0    */package designptn.interpreter;import java.util.ArrayList;import java.util.List;public class Context {private String context;private List
 
   list = new ArrayList
  
   ();public void setContent(String content) {this.context = content;}public String getContent() {return this.context;}public void add(Expression eps) {list.add(eps);}public List
   
     getList() {return list;}}
   
  
 

/**    * @author oscar999    * @date 2015-1-6 * @version V1.0    */package designptn.interpreter;public class Type1Expression extends Expression {void interpret(Context ctx) {// TODO Auto-generated method stubSystem.out.println("Interpret Type1 content!");}}

/**    * @author oscar999    * @date 2015-1-6 * @version V1.0    */package designptn.interpreter;public class Type2Expression extends Expression {void interpret(Context ctx) {// TODO Auto-generated method stubSystem.out.println("Interpret Type2 content!");}}

/**    * @author oscar999    * @date 2015-1-6 * @version V1.0    */package designptn.interpreter;/** * @author Administrator * */public class TestMain {public static void main(String[] args) {// TODO Auto-generated method stubContext ctx = new Context();ctx.add(new Type1Expression());ctx.add(new Type2Expression());for(Expression eps : ctx.getList()){eps.interpret(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.