Design Mode entry - simple factory pattern

Foreword

Design patterns are the words of the problems we often encounter in the interview, but also a lot of development encountered. Before always wanted to learn the design mode, and then summarize a bit of writing, but has been busy with other things, and now finally learn to design mode, the reference book is "lying Design Mode", this book is written in plain, I recommend everyone take a look .

Scene Description

Borrow the book sample questions, plus calculate two numbers, subtract, multiply, in addition to other operations, the general thinking is that we deal with the problem from top to bottom, it will cause the two numbers after several judgments.

Scene Example

Two numbers before arithmetic operation, as shown in the following code:

package me.xueyao;
/**
 * @author Simon.Xue
 * @date 2019-12-30 22:54
 **/
public class OperationHello {

    public static void main(String[] args) {
        System.out.println(getResult("*", 1, 3));
    }

    public static int getResult(String operationStr, int number1, int number2) {
        int result = 0;
        switch (operationStr) {
            case "+":
                result = number1 + number2;
                break;
            case "-":
                result = number1 - number2;
                break;
            case "*":
                result = number1 * number2;
                break;
            case "/":
                result = number1 / number2;
                break;
        }
        return result;
    }
}

The code here to judge four times, but if you add other operations, it will be added conditional judgment based on the original. Later, with the expansion of business there will continue to add to determine conditions. Coupling of the code will be higher, if you modify a problem at the operation, causing online business can not access the functions related to the operation, must be modified good question, other computing functions can be used.

Solution

We can now use what we call a simple factory pattern, first of all, we want to, add, subtract, multiply, and divide operations belong to the operator class, so we create a parent class operation Operation .class

package me.xueyao;

/**
 * @author Simon.Xue
 * @date 2019-12-30 22:18
 **/
public abstract class Operation {

    private int number1;
    private int number2;

    public int getNumber1() {
        return number1;
    }

    public void setNumber1(int number1) {
        this.number1 = number1;
    }

    public int getNumber2() {
        return number2;
    }

    public void setNumber2(int number2) {
        this.number2 = number2;
    }

    public abstract int getResult();
}

Addition, subtraction, multiplication, and division arithmetic, we were created OperationAdd.class, OperationSub.class, OperationMul.class, OperationDiv.class four subclasses inherit the parent class operation Operation.class, where the calculation methods.

package me.xueyao;

/**
 * @author Simon.Xue
 * @date 2019-12-30 22:22
 **/
public class OperationAdd extends Operation {

    @Override
    public int getResult() {
        return getNumber1() + getNumber2();
    }
}
package me.xueyao;

/**
 * @author Simon.Xue
 * @date 2019-12-30 22:22
 **/
public class OperationSub extends Operation {

    @Override
    public int getResult() {
        return getNumber1() - getNumber2();
    }
}
package me.xueyao;

/**
 * @author Simon.Xue
 * @date 2019-12-30 22:25
 **/
public class OperationMul extends Operation {
    @Override
    public int getResult() {
        return getNumber1() * getNumber2();
    }
}
package me.xueyao;

/**
 * @author Simon.Xue
 * @date 2019-12-30 22:26
 **/
public class OperationDiv extends Operation {
    @Override
    public int getResult() {
        return getNumber1() / getNumber2();
    }
}

When we use the calculation method, only you need to create their objects, and then perform the calculation, you can calculate two numbers, but now there is a problem that must be the way to create an object for each operation. Be exposed parent class object, we can create a factory class, the class factory to create the required objects, the following code as shown

package me.xueyao;

/**
 * @author Simon.Xue
 * @date 2019-12-30 22:30
 **/
public class OperationFactory {

    public static Operation createOperation(String operationStr) {
        Operation operation = null;

        switch (operationStr) {
            case "+":
                operation = new OperationAdd();
                break;
            case "-":
                operation = new OperationSub();
                break;
            case "*":
                operation = new OperationMul();
                break;
            case "/":
                operation = new OperationDiv();
                break;
        }
        return operation;
    }
}

to sum up

Simple factory pattern is a design pattern in a pattern relatively simple, but it is every extra a need, we must create a class, in general, with up pretty good. Articles If you have questions, please contact me.

Guess you like

Origin blog.51cto.com/flowstone/2463235