Did you give out the year-end bonus?

Posted by joey3002 on Fri, 04 Feb 2022 09:22:53 +0100

The year-end bonus is not only the happiness of many people, but also the pain of many people.

Of course, whether happiness is destined for you depends on whether happiness is destined for you. This is a metaphysics.

In some companies, if one person has a year-end bonus, others will have it. In some companies, it is based on your performance throughout the year.

For example, there is a company:

The rules for their year-end bonus are as follows: only those who are hardworking and positive, and whose names are Xiaoming, Xiaoliang and Xiaolu.

For example, hardworking Xiaoliang and active Xiaoming will be given a year-end bonus.

Others, such as lazy Xiao Ming and industrious Xiao Li, are not given.

The expression is used to indicate whether to pay the year-end bonus. It is as follows:

(hardworking people 𞓜 positive people) & & (Xiaoming | Xiaoliang | Xiaoxiao)

Let's use this example today to tell you about a design pattern - Interpreter pattern

1, What is it

The Interpreter Pattern provides the syntax of the evaluation language. This pattern implements an expression interface that interprets a specific context.

This design pattern is mostly used in SQL parsing, symbol processing engine and so on.

It looks like this:

I understand this:

The other is to classify our if else according to certain rules, pass the conditions into the parsing engine, and then return the calculation results.

2, Realize

IExpression

package interpreter;

/**
 * @author Day and night programming of Muzi
 * Rule abstraction
 */
public interface IExpression {
    public boolean interpret(String data);
}

TerminalExpression

package interpreter;

import java.util.Arrays;
import java.util.List;

/**
 * @author Day and night programming of Muzi
 * The terminating expression conforms to the conditions
 */
public class TerminalExpression implements  IExpression{

    // Define conditions
    List<String> conditions;
    public TerminalExpression(String ... args){
        conditions =  Arrays.asList(args);
    }
    // Judge whether it is diligent, positive, Xiao Ming, Xiao Liang, etc
    @Override
    public boolean interpret(String data) {
        if (data == null) {
            return false;
        }
        return conditions.contains(data);
    }
}

NoTerminalExpression

package interpreter;

/**
 * @author Day and night programming of Muzi
 */
public class NoTerminalExpression implements IExpression {
    IExpression expression01;
    IExpression expression02;
    // A non terminating expression requires a terminating expression to be called
    public NoTerminalExpression(IExpression expression1, IExpression expression2){
        this.expression01 = expression1;
        this.expression02 = expression2;
    }
    @Override
    public boolean interpret(String data) {
        String[] arrs = data.split("of");
        if (arrs == null || arrs.length != 2) {
            return false;
        }
        // Judge whether the two conditions are met at the same time
        return expression01.interpret(arrs[0]) && expression02.interpret(arrs[1]);
    }
}

Context

​```package interpreter;

/**
 * @author Day and night programming of Muzi
 */
public class Context {
    private String[] types = { "diligence", "positive"};
    private String[] names = { "Xiao Ming", "Xiao Liang", "path"};
    // Best expression
    IExpression expression;
    public Context() {
        IExpression typeExpression = new TerminalExpression(types);
        IExpression nameExpression = new TerminalExpression(names);
        expression = new NoTerminalExpression(typeExpression, nameExpression);
    }

    //Call the interpretation method of the related expression class
    public void freeRide(String info) {
        boolean ok = expression.interpret(info);
        if(ok){
            System.out.println("to"+info+"Year end bonus!");
        } else {
            System.out.println("No"+info+"Year end bonus!");
        }
    }
}

Client

package interpreter;

/**
 * @author Day and night programming of Muzi
 */
public class Client {
    public static void main(String[] args) {
        Context context = new Context();
        context.freeRide("Diligent Xiao Liang");
        context.freeRide("Positive Xiao Ming");
        context.freeRide("Lazy little Li");
        context.freeRide("Lazy Xiao Ming");
    }
}

Output result:

These types of relationships:

3, Nagging

Interpreter mode seems to be rarely seen at ordinary times, and you can learn from it if you see it less.

There are few rich people. Don't you still want to be rich, ha ha!

There are two days left in the holiday. Cherish it!

Topics: Java Design Pattern