Introduction to design mode: Interpreter Pattern)

Source: Internet
Author: User

Introduction to design mode: Interpreter Pattern)

 
What is the interpreter mode?

Official explanation: to define a representation of grammar of a given language, along with an interpreter that uses this representation to interpret sentences in the language.
Defines the syntax expression of a given language and uses the expression as an interpreter to explain sentences in a language.

General explanation:Given a language and related syntax, the client can define an interpreter for a syntax expression based on these syntaxes to interpret sentences in this language.

Why is the interpreter mode used?

Syntax expressions are abstracted and encapsulated, which is easy to modify and expand. When a new feature is added to the language, you can inherit the abstract expression class to implement new language features.

Each syntax can be expressed as an expression class, which is easier to implement.

PS: This mode is difficult to maintain complex syntaxes due to its structural characteristics and has low execution efficiency. Therefore, this mode is hardly applicable in actual development, however, its own structure and ideas can be used for reference.

How to Use the interpreter mode?
The UML diagram is as follows:
VcfJq6Oos + Platform + nP87HttO/Kvb3HyavL + dKqx/platform/Platform + 38zl1tW94bHttO/Platform + 7 HttO/Platform + 7 HttO/platform/LV38bky/Platform + workshop/terminal/ release/release + 8v5ttTTprXEvt/release + release/release + s73Hyau + release/OjrLj40 + jSu7j219a3 + release/Kx7fxysfK/dfW19a3 + release/release + "brush: java; ">interface AbstractExpression { boolean interpret(Character character);}

2. Define an ending expression to determine whether a character is a numeric character.

Public class TerminalExpression implements extends actexpression {@ Override public boolean interpret (Character character) {// whether the return character. isDigit (character );}}

3. Define a simple non-ending expression, and, not, or

public class AndExpression implements AbstractExpression {    private AbstractExpression leftExpression;    private AbstractExpression rightExpression;    public AndExpression(AbstractExpression leftExpression, AbstractExpression rightExpression) {        this.leftExpression = leftExpression;        this.rightExpression = rightExpression;    }    @Override    public boolean interpret(Character character) {        return leftExpression.interpret(character) && rightExpression.interpret(character);    }}public class NotExpression implements AbstractExpression {    private AbstractExpression expression;    public NotExpression(AbstractExpression expression) {        this.expression = expression;    }    @Override    public boolean interpret(Character character) {        return !expression.interpret(character);    }}public class OrExpression implements AbstractExpression {    private AbstractExpression leftExpression;    private AbstractExpression rightExpression;    public OrExpression(AbstractExpression leftExpression, AbstractExpression rightExpression) {        this.leftExpression = leftExpression;        this.rightExpression = rightExpression;    }    @Override    public boolean interpret(Character character) {        return leftExpression.interpret(character) || rightExpression.interpret(character);    }}

4. The client is used. The context component is not required because it is relatively simple:

public class Client {    public static void main(String[] args) {        Character digitCharacter = new Character('1');        Character notdigitCharacter = new Character('l');        AbstractExpression terminalExpression = new TerminalExpression();        AbstractExpression notExpression = new NotExpression(terminalExpression);        AbstractExpression andExpression = new AndExpression(terminalExpression, notExpression);        AbstractExpression orExpression = new OrExpression(terminalExpression, notExpression);        System.out.println(andExpression.interpret(digitCharacter));        System.out.println(andExpression.interpret(notdigitCharacter));        System.out.println(orExpression.interpret(digitCharacter));        System.out.println(orExpression.interpret(notdigitCharacter));    }}

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.