Simple factory model of design pattern learning

The simple factory mode, in fact, has a static factory method. You can return the corresponding instance according to the output conditions, and then perform the calculation with the specific instance method.

The simple factory pattern is equivalent to centralizing condition judgment into a static factory method. The advantage of this is that the code that performs the specific calculation task does not need to have a lot of if judgment, and only focuses on the specific calculation. If there is a new calculation requirement, only the corresponding calculation method needs to be added, and the corresponding entry conditions need to be added in the factory method, which has no impact on the original calculation method.

An example of a simple calculator is as follows

Calculated type interface:

/**
 * @author wgyang
 * Create time: 2020-04-04 15:34
 * Description:
 */
public interface Operator {

    /**
     * Calculation
     *
     * @param number1 Number 1
     * @param number2 Number 2
     * @return Return the result of calculation
     */
    String calculate (int number1, int number2);
}

Four operations:

/**
 * @author wgyang
 * Create time: 2020-04-04 15:44
 * Description: Addition operation
 */
public class OperatorAdd implements Operator {
    public String calculate(int number1, int number2) {
        return String.valueOf(number1 + number2);
    }
}
/**
 * @author wgyang
 * Create time: 2020-04-04 15:48
 * Description: Subtraction operation
 */
public class OperatorSubtraction implements Operator {
    public String calculate(int number1, int number2) {
        return String.valueOf(number1 - number2);
    }
}
/**
 * @author wgyang
 * Create time: 2020-04-04 15:49
 * Description: Multiplication operation
 */
public class OperatorMultiplication implements Operator {
    public String calculate(int number1, int number2) {
        return String.valueOf(number1 * number2);
    }
}
/**
 * @author wgyang
 * Create time: 2020-04-04 15:50
 * Description: Departure operation
 */
public class OperatorDivision implements Operator {
    public String calculate(int number1, int number2) {
        if (number2 == 0) {
            return "Divisor cannot be 0";
        } else {
            return String.valueOf(number1 / number2);
        }
    }
}

Static factory class:

/**
 * @author wgyang
 * Create time: 2020-04-04 15:56
 * Description: Factory class for operation
 */
public class OperatorFactory {
    public static Operator createOperator(String operate) {
        switch (operate) {
            case "+":
                return new OperatorAdd();
            case "-":
                return new OperatorSubtraction();
            case "*":
                return new OperatorMultiplication();
            case "/":
                return new OperatorDivision();
            default:
                throw new UnsupportedOperationException(String.format("Unsupported operation type:%s", operate));
        }
    }
}

Calculator class:

/**
 * @author wgyang
 * Create time: 2020-04-04 15:32
 * Description: To realize a simple calculator, input two integers, add, subtract, multiply and divide them, and use the simple factory design mode
 */
public class Calculator {
    public static void main(String[] args) {
        int number1 = 2;
        int number2 = 5;
        Operator operator = OperatorFactory.createOperator("+");
        System.out.println(operator.calculate(number1, number2));
        Operator operator1 = OperatorFactory.createOperator("-");
        System.out.println(operator1.calculate(number1, number2));
        Operator operator2 = OperatorFactory.createOperator("*");
        System.out.println(operator2.calculate(number1, number2));
        Operator operator3 = OperatorFactory.createOperator("/");
        System.out.println(operator3.calculate(number1, number2));
        Operator operator4 = OperatorFactory.createOperator("%");
        System.out.println(operator4.calculate(number1, number2));
    }
}

In this example, only four arithmetic operations of addition, subtraction, multiplication and division of calculator are implemented. If you want to add arithmetic operations such as multiplication and division, you only need to implement Operator interface and add corresponding entry in createOperator() method, which will not affect the original implementation of addition, subtraction, multiplication and division.

Keywords: Java calculator

Added by Monk3h on Sun, 05 Apr 2020 12:32:46 +0300