Simple Factory model

  • Basic idea: divide and mould
  • Purpose: expandability, flexibility

tips

  • Firstly, the commonness of attributes and methods of the object should be abstracted, and then the independence of each module should be considered from the perspective of expansibility and flexibility

  • In the calculator project, for example, we abstracted out that each computer operation consisted of the number1,number2 operators, so we could write a class as a parent and let the add, subtract, subtract, multiply, and divide classes inherit

  • Why not use an interface, because interfaces don’t support defining attributes

analogy

  • Let’s say you have a factory that produces cars, motorcycles, bicycles, cars. Factory class external encapsulation, when the customer incoming car type, will eventually be delivered out of the corresponding car type

code

The parent class code

public class Operation {
    public Long NumberA;
    public Long NumberB;

    public Long getNumberA(a) {
        return NumberA;
    }

    public void setNumberA(Long numberA) {
        NumberA = numberA;
    }

    public Long getNumberB(a) {
        return NumberB;
    }

    public void setNumberB(Long numberB) {
        NumberB = numberB;
    }

    public Long GetResult(a){
        Long result;
        result = 0L;
        returnresult; }}Copy the code

Add, subtract, multiply, and divide subclass inheritance

  • add
public class OperationAdd extends Operation {

// public Long getNumberA() {
// return NumberA;
/ /}
//
// public void setNumberA(Long numberA) {
// NumberA = numberA;
/ /}
//
// public Long getNumberB() {
// return NumberB;
/ /}
//
// public void setNumberB(Long numberB) {
// NumberB = numberB;
/ /}

    @Override
    public Long GetResult(a) {
        Long result = 0L;
        result = NumberA + NumberB;
        returnresult; }}Copy the code
  • division

    public class OperationDiv extends Operation {
    
        @Override
        public Long GetResult(a) {
            Long result = 0L;
            if (NumberB == 0) try {
                throw new Exception("The divisor cannot be zero.");
            } catch (Exception e) {
                e.printStackTrace();
            }
            result = NumberA / NumberB;
            returnresult; }}Copy the code
  • The multiplication

    public class OperationMul extends Operation {
    
        @Override
        public Long GetResult(a) {
            Long result = 0L;
            result = NumberA * NumberB;
            returnresult; }}Copy the code
  • subtraction

    public class OperationSub extends Operation { @Override public Long GetResult() { Long result = 0L; result = NumberA - NumberB; return result; }}Copy the code

The factory class encapsulates the call operation subclass

public class OperationFactory {

    public static Operation CreateOperate(String operate){
        Operation oper = null;
        switch (operate){
            case "+":
                oper = new OperationAdd();
                break;
            case "-":
                oper = new OperationSub();
                break;
            case "*":
                oper = new OperationMul();
                break;
            case "/":
                oper = new OperationDiv();
                break;

        }
        returnoper; }}Copy the code

Advocate tone with class

public class Main{

        public static void main(String[]args){
/* OperationAdd calc = new OperationAdd(); calc.setNumberA(10L); calc.NumberB = 30L; System.out.println(calc.GetResult()); * /
                Operation oper;
                oper = OperationFactory.CreateOperate("-");
                System.out.println("Call subtraction class");
                oper.NumberA = 15L;
                oper.NumberB = 10L; System.out.println(oper.GetResult()); }}Copy the code