Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

define

Given a language, define its grammatical representation and define an interpreter that uses the identifier to interpret sentences in the language.

It is more niche than the command pattern and is used only in Specific domains, such as compilers, rule engines, regular expressions, SQL, etc., which are also known as Domain Specific languages (DSLS).

It’s not often that we’re asked to write an interpreter, just to learn about it, and to learn from ideas about how to express complex logic in simpler rules.

The advantages and disadvantages

Advantages: 1. Good scalability and flexibility. 2. Added new ways to interpret expressions. 3. Easy to implement simple grammar.

Disadvantages: 1. Less available scenarios. 2. It is difficult to maintain complex grammar. Interpreter schema causes class bloat. 4. The interpreter mode uses recursive invocation methods.

The specific implementation

Step 1

Create an expression interface.

public interface Expression {
    public boolean interpreter(String content);
}
Copy the code

Step 2

Create an entity class that implements the above interface.

public class TerminalExpression implements Expression {

    private String data;

    public TerminalExpression(String data) {
        this.data = data;
    }

    @Override
    public boolean interpreter(String content) {
        returncontent.contains(data); }}Copy the code
public class OrExpression implements Expression {

    private Expression expression1;
    private Expression expression2;

    public OrExpression(Expression expression1, Expression expression2) {
        this.expression1 = expression1;
        this.expression2 = expression2;
    }

    @Override
    public boolean interpreter(String content) {
        returnexpression1.interpreter(content) || expression2.interpreter(content); }}Copy the code
public class AndExpression implements Expression {

    private Expression expression1;
    private Expression expression2;

    public AndExpression(Expression expression1, Expression expression2) {
        this.expression1 = expression1;
        this.expression2 = expression2;
    }

    @Override
    public boolean interpreter(String content) {
        returnexpression1.interpreter(content) && expression2.interpreter(content); }}Copy the code

Step 3

The InterpreterPatternDemo uses the Expression class to create rules and parse them.

public class InterpreterPatternDemo {

    // Rule: Robert and John are male
    public static Expression getMaleExpression(a) {
        Expression robert = new TerminalExpression("Robert");
        Expression john = new TerminalExpression("John");
        return new OrExpression(robert, john);
    }

    // Rules: Julie is a married woman
    public static Expression getMarriedWomanExpression(a) {
        Expression julie = new TerminalExpression("Julie");
        Expression married = new TerminalExpression("Married");
        return new AndExpression(julie, married);
    }

    public static void main(String[] args) {
        Expression isMale = getMaleExpression();
        Expression isMarriedWoman = getMarriedWomanExpression();

        System.out.println("John is male? " + isMale.interpreter("John"));
        System.out.println("Julie is a married women? "
                + isMarriedWoman.interpreter("Married Julie")); }}Copy the code

Step 4

Execute the program and output the result:

John is male? true
Julie is a married women? true
Copy the code

conclusion

The interpreter is a little-used pattern, and if you do encounter a situation where a particular type of problem occurs frequently enough, you are advised to give preference to mature third-party, open source parsing tools when using the interpreter pattern.