Interpreter mode (interpreter pattern)

Source: Internet
Author: User

Interpreter mode: Creates an interpreter for the language, providing a way to evaluate the language's syntax or expression.

Example:

 Public InterfaceExpression { Public Abstract Booleaninterpret (String context); Public classTerminalexpressionImplementsExpression {PrivateString data;  Publicterminalexpression (String data) { This. data =data; } @Override Public Booleaninterpret (String context) {if(Context.contains (data)) {return true; }        return false; }} Public classOrexpressionImplementsExpression {PrivateExpression Exp1; PrivateExpression Exp2;  Publicorexpression (expression exp1, expression Exp2) { This. EXP1 =EXP1;  This. EXP2 =EXP2; } @Override Public Booleaninterpret (String context) {returnExp1.interpret (context) | |Exp2.interpret (context); }} Public classAndexpressionImplementsExpression {PrivateExpression Exp1; PrivateExpression Exp2;  Publicandexpression (expression exp1, expression Exp2) { This. EXP1 =EXP1;  This. EXP2 =EXP2; } @Override Public Booleaninterpret (String context) {returnExp1.interpret (context) &&Exp2.interpret (context); }} Public classTest { Public Staticexpression getmaleexpression () {expression Robert=NewTerminalexpression ("Robert"); Expression John=NewTerminalexpression ("John"); return Neworexpression (Robert, John); }         Public Staticexpression getmarriedwomanexpression () {expression Julie=NewTerminalexpression ("Julie"); Expression married=NewTerminalexpression ("Married"); return Newandexpression (julie,married); }     Public Static voidMain (string[] args) {Expression Ismale=getmaleexpression (); System.out.println (Ismale.interpret ("John")); Expression Ismarriedwoman=getmarriedwomanexpression (); System.out.println (Ismarriedwoman.interpret ("Married Julie")); }}

Test results:

In Java, where there is little use of the interpreter pattern, the EXPRESSION4J framework is a Java-based open-source framework that operates on mathematical expressions and is a mathematical equation parser. It is the best example of applying the interpreter pattern, but the framework is still in the process of being perfected.

Interpreter mode (interpreter pattern)

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.