Simple factory model

1. Simple factory model

  • Example: Let us write a method of addition, subtraction, multiplication, and division. The initial wording is: (For the convenience of the denominator when the division is not determined 0)
public class Operation {
    
    
    public static double getReault(double left ,double right,String op) {
    
    
        double result = 0;
        switch (op) {
    
    
            case "+":
                result = left+right;
                break;
            case "-":
                result = left-right;         
                break;
            case "*":
                result = left*right;
                break;
            case "/":
                result = left/right;
                break;
            default:
                break;
        }
        return result;
    }
}

Although this method extracts functions to a certain extent, it is not very beneficial for increasing calculations.

  • When we use inheritance features, for example:
//首先定义一个父类
public class Operation {
    
    
    private double left;
    private double right;
    public double getLeft() {
    
    
        return left;
    }
    public void setLeft(double left) {
    
    
        this.left = left;
    }
    public double getRight() {
    
    
        return right;
    }
    public void setRight(double right) {
    
    
        this.right = right;
    }
    public double getReault() {
    
    
        return 0;
    }    
}
//第二步,创建加减乘除四个类继承Operation
public class OperationAdd extends Operation {
    
    
    @Override
    public double getReault() {
    
    
        return this.getLeft() + this.getRight();
    }  
}

public class OperationSub extends Operation {
    
    
    @Override
    public double getReault() {
    
    
        return this.getLeft() - this.getRight();
    }
}

public class OperationMul extends Operation {
    
    
    @Override
    public double getReault() {
    
    
        return this.getLeft() * this.getRight();
    }
}

public class OperationDiv extends Operation {
    
    
    @Override
    public double getReault() {
    
    
        return this.getLeft() / this.getRight();
    }    
}
//定义工厂类
public class OperationFactory {
    
    
    public static Operation craeteOperation(String op) {
    
    
        Operation operation = null;
        switch (op) {
    
    
            case "+":
                operation = new OperationAdd();
                break;
            case "-":
                operation = new OperationSub();
                break;
            case "*":
                operation = new OperationMul();
                break;
            case "/":
                operation = new OperationDiv();
                break;
            default:
                break;
        }
        return operation;      
    }
}

//主函数测试类
public class Test {
    
    
    public static void main(String[] args) {
    
    
        Operation operation = OperationFactory.craeteOperation("+");
        operation.setLeft(1);
        operation.setRight(2);
        System.out.println(operation.getReault());
    }
}

At this time, we can dynamically create objects through the factory according to the input symbols to complete the addition, subtraction, multiplication, and division operations;

If there are new requirements, such as adding sqrtfunctions, you only need to create a class inheritance Operation. At first glance, the amount of code seems to increase, but the scalability of the code increases.

  • Since the simple factory pattern can be implemented through inherited classes, so can interfaces. In this case, the interface may not have less code than the inherited class.
//首先定义一个接口
public interface Operation {
    
    
    //将公共方法抽出定义在接口中
    double getResult(double left,double right);
}
//加
public class OperationAdd implements Operation {
    
    
    @Override
    public double getResult(double left, double right) {
    
    
        return left + right;
    }
}
//减
public class OperationSub implements Operation {
    
    
    @Override
    public double getResult(double left, double right) {
    
    
        return left - right;
    }
}
//乘
public class OperationMul implements Operation {
    
    
    @Override
    public double getResult(double left, double right) {
    
    
        return left * right;
    }
}
//除
public class OperationDiv implements Operation {
    
    
    @Override
    public double getResult(double left, double right) {
    
    
        return left / right;
    }
}

//定义的工厂类没有什么变化
public class OperationFactory {
    
    
    public static Operation craeteOperation(String op) {
    
    
        Operation operation = null;
        switch (op) {
    
    
            case "+":
                operation = new OperationAdd();
                break;
            case "-":
                operation = new OperationSub();
                break;
            case "*":
                operation = new OperationMul();
                break;
            case "/":
                operation = new OperationDiv();
                break;
            default:
                break;
        }
        return operation;      
    }
}
//主函数测试类
public class Test {
    
    
    public static void main(String[] args) {
    
    
        Operation operation = OperationFactory.craeteOperation("+");
        System.out.println(operation.getResult(1, 3));
    }
}

Reference material: Big talk design pattern, by Cheng Jie

Guess you like

Origin blog.csdn.net/qq_37771811/article/details/103755554