Design pattern one, simple factory pattern

Using a separate OperationFactory class to do the process of creating Operation operator instances is the simple factory pattern.

The addition, subtraction, multiplication, and division operators all use the object-oriented inheritance feature. If you need to add a square root or sin function operation, you only need to inherit one more without changing the original code. To change one of them, you only need to change it in its own class.

The encapsulation feature is reflected in the separation of the operation class and the client interface display

It can also be implemented with an interface, which is more flexible. The usage of the simple factory pattern is mainly reflected here

//运算符基类
public class Operation {
    
    
    protected double _numA;
    protected double _numB;
    protected double _result;

    public double get_numA() {
    
    
        return _numA;
    }

    public void set_numA(double _numA) {
    
    
        this._numA = _numA;
    }

    public double get_numB() {
    
    
        return _numB;
    }

    public void set_numB(double _numB) {
    
    
        this._numB = _numB;
    }

    public double GetResult() throws Exception {
    
    
        return _result;
    }
}

//运算符子类,重写运算方法
public class OperationAdd extends Operation{
    
    
    @Override
    public double GetResult() {
    
    
        return this._numA+this._numB;
    }
}

public class OperationSub extends Operation{
    
    
    @Override
    public double GetResult() {
    
    
        return this._numA-this._numB;
    }
}

public class OperationMul extends Operation{
    
    
    @Override
    public double GetResult() {
    
    
        return this._numA*this._numB;
    }
}

public class OperationDiv extends Operation {
    
    
    @Override
    public double GetResult() throws Exception {
    
    
        if (this._numB == 0) {
    
    
            throw new Exception("除数不能为0");
        }
        return this._numA / this._numB;
    }
}

Simple factory pattern

//简单工厂模式,用于创造运算符实例
public class OperationFactory {
    
    
    public static Operation CreateOperation(String operation) {
    
    
        Operation oper = null;
        switch (operation) {
    
    
            case "+":
                oper = new OperationAdd();
                break;
            case "-":
                oper = new OperationSub();
                break;
            case "*":
                oper = new OperationMul();
                break;
            case "/":
                oper = new OperationDiv();
                break;
        }
        return oper;
    }

    //界面和运算业务分离,封装特性
    public static void main(String[] args) {
    
    
        try {
    
    
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入数字A");
            String numA = sc.next();
            System.out.println("请输入运算符 + - * /");
            String operation = sc.next();
            System.out.println("请输入数字B");
            String numB = sc.next();

            Operation oper = OperationFactory.CreateOperation(operation);
            oper.set_numA(Double.valueOf(numA));
            oper.set_numB(Double.valueOf(numB));

            System.out.printf("结果是  %f", oper.GetResult());
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

}



Guess you like

Origin blog.csdn.net/weixin_45401129/article/details/114628947