当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 设计模式学习笔记 ———— 简单工厂模式

设计模式学习笔记 ———— 简单工厂模式

2019年12月03日  | 移动技术网IT编程  | 我要评论

# 背景  

  简单工厂模式是很多程序员学习的第一个设计模式,因为其不但原理简单而且易于上手,在日常工作的代码中也常有体现。今天分享一个基于实现“加”、“减”、“乘”、“除”计算器的需求基于简单工厂模式来实现。

# 错误示范

  在学习简单工厂模式之前,遇到这种需求我是这样实现的:

public static double calculator(double num1, double num2, string symbol) throws exception {
        double result = 0.0d;
        if (symbol == null || num1 == null || num2 == null) {
            return result;
        }
        switch (symbol) {
            case "+":
                result = num1 + num2;
                break;
            case "-":
                result = num1 - num2;
                break;
            case "*":
                result = num1 * num2;
                break;
            case "/":
                if (num2 == 0) {
                    throw new exception("使用除法的时候分母不能为0");
                }
                result = num1 / num2;
                break;
            default:{}

        }
        return result;
    }

  简单来说是船舰一个方法,然后通过switch case 一个一个的去判断,然后对比中的进行处理,后续如果有其他判断的话,就继续在下面在增加case判断。那么这段代码实现了需求了,是实现了,但是这段代码是优秀的吗?不是。为什么呢?例如现在客户需求增加一个平方算法,我们在case中增加这个算法,却需要加减乘除的运算都得来参与编译,在这个代码的迭代过程中,如果不小心对原有代码进行了误操作,会造成很糟糕的结果,尤其是这个误操作有可能还不会报错……所以我们应该把加减乘除等运算进行分离,修改其中一个不会影响另外的几个,增加平方算法也不影响其他代码。

  声明父类

@data
public abstract class operation {

    private double num1;
    private double num2;

    protected abstract double getresult();
   
}

  创建多个计算类用于继承父类并重写getresult方法,这里我分别创建了加减乘除四个类来继承operation类,并重写getresult()方法

class operationadd extends operation{

    @override
    protected double getresult() {
        return getnum1() + getnum2();
    }
}

class operationsub extends operation {

    @override
    protected double getresult() {
        return getnum1() - getnum2();
    }
}

class operationmul extends operation {

    @override
    protected double getresult() {
        return getnum1() * getnum2();
    }
}

class operationdiv extends operation {

    @override
    protected double getresult() {
        return getnum1() / getnum2();
    }
}

  此时,我们只需要简单的写一个工程方法就可以实现业务要求了。这里只是实例代码,很多细节判断没有加,如果是在生成环境中,首先入参肯定要校验是否为null,如果为null会报npe,上面除法运算类中,应该对分母进行判断,是否为0等等……

class operationfactory {
    public static operation createoperation(string operate) {
        operation operation;
        switch (operate) {
            case "+":
                operation = new operationadd();
                break;
            case "-":
                operation = new operationsub();
                break;
            case "*":
                operation = new operationmul();
                break;
            case "/":
                operation = new operationdiv();
                break;
            default:{
                operation = new operationadd();
            }
        }
        return operation;
    }
}

  测试main方法

public static void main(string[] args) {
        operation operation = operationfactory.createoperation("*");
        operation.setnum1(11d);
        operation.setnum2(1.2);
        system.out.println(operation.getresult());
}    

  返回结果:

13.2

 

  

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网