设计模式-简单工厂设计模式

2016-01-28  本文已影响0人  yangxuan

设计模式-简单工厂设计模式

package lesson1;

public class Lession1 {
    
    public static double getResult(double numberA, double numberB, String operater){
        double result = 0;
        // 切记将未知变量进行equals比较,如果变量为Null,则程序异常(空指针 异常)
        /*if(operater.equals("+")){
            result = numberA + numberB;
        }*/
        if("+".equals(operater)){
            result = numberA + numberB;
        }
        if("-".equals(operater)){
            result = numberA - numberB;
        }
        if("*".equals(operater)){
            result = numberA * numberB;
        }
        if("/".equals(operater)){
            result = numberA / numberB;
        }
        
        return result;
    }
    
    public static void main(String[] args) throws Exception{
        double result = getResult(100, 1, "+");
        System.out.println("计算结果:"+result);
        
        result = getResult(100, 1, "-");
        System.out.println("计算结果:"+result);
        
        result = getResult(100, 1, "*");
        System.out.println("计算结果:"+result);
        
        result = getResult(100, 1, "/");
        System.out.println("计算结果:"+result);
        
        //如果不存在
        result = getResult(100, 1, "&");
        System.out.println("计算结果:"+result);
        
        //除数异常
        result = getResult(100, 0, "/");
        System.out.println("计算结果:"+result);
    }
}

在上面程序中:

    上述程序是一个加减乘除计算方法,其中getResult方法返回值为结果;但是可以看出程序有不健壮性,异常处理等为考虑周全,如果需要加校验,可能需要在方法体内加,代码比较臃肿;
        -可维护:
        -可复用
        -可扩展
        -灵活性好

接口定义,提供getResult方法,进行抽象

package lesson1_1;

public interface IOperation {
    
    double getResult(MathParams mathParams);

}

加法实现类

package lesson1_1;

/**
 * 加法
 * @author PRO
 *
 */
public class OperationAdd implements IOperation{

    @Override
    public double getResult(MathParams mathParams) {
        return mathParams.getNumberA() + mathParams.getNumberB();
    }

}

减法实现类

package lesson1_1;

/**
 * 减法
 * @author PRO
 *
 */
public class OperationSub implements IOperation{

    @Override
    public double getResult(MathParams mathParams) {
        return mathParams.getNumberA() - mathParams.getNumberB();
    }

}

乘法实现类

package lesson1_1;

/**
 * 乘法
 * @author PRO
 *
 */
public class OperationMul implements IOperation{

    @Override
    public double getResult(MathParams mathParams) {
        return mathParams.getNumberA() * mathParams.getNumberB();
    }

}

除法实现类

package lesson1_1;

/**
 * 除法
 * @author PRO
 *
 */
public class OperationDiv implements IOperation{
    
    private static void check(MathParams mathParams){
        if (mathParams.getNumberB() == 0) {
            throw new RuntimeException("除法运算(除数不能为0)");
        }
    }

    @Override
    public double getResult(MathParams mathParams){
        check(mathParams);
        return mathParams.getNumberA() / mathParams.getNumberB();
    }

}

计算参数类

package lesson1_1;

/**
 * 计算参数
 * 
 * @author PRO
 *
 */
public class MathParams {

    private double numberA;
    private double numberB;

    public double getNumberA() {
        return numberA;
    }

    public void setNumberA(double numberA) {
        this.numberA = numberA;
    }

    public double getNumberB() {
        return numberB;
    }

    public void setNumberB(double numberB) {
        this.numberB = numberB;
    }

}

操作符枚举

package lesson1_1;

/**
 * 操作符枚举
 * @author PRO
 *
 */
public enum OperateEnum {
    
    ADD,
    
    SUB,
    
    MUL,
    
    DIV;

}

操作工厂类

package lesson1_1;

/**
 * 操作代理类
 * @author PRO
 *
 */
public class OperationFactory {
    
    public static IOperation createOperation(OperateEnum operateEnum){
        IOperation operation = null;
        
        switch (operateEnum) {
        case ADD:
            operation = new OperationAdd();
            break;
        case SUB:
            operation = new OperationSub();
            break;
        case MUL:
            operation = new OperationMul();
            break;
        case DIV:
            operation = new OperationDiv();
            break;
        }
        return operation;
        
    }

}

程序入口

package lesson1_1;

public class Main {
    
    
    public static double getResult(double numberA,double numberB,OperateEnum operateEnum){
        IOperation operation = OperationFactory.createOperation(operateEnum);
        MathParams mathParams = new MathParams();
        mathParams.setNumberA(numberA);
        mathParams.setNumberB(numberB);
        return operation.getResult(mathParams);
    }
    
    public static void main(String[] args){
        double result = getResult(1, 2, OperateEnum.SUB);
        System.out.println(result);
        result = getResult(1, 0, OperateEnum.DIV);
        System.out.println(result);
    }
    
}

    上述之后的代码,用到了Java面向对象中抽象(多态),分装
    -易维护:程序将加减程序都单独抽离开,各自有各自的实现,如果需要修改程序,只需要维护各自实现的getResult方法;
    -易扩展:程序后期如果需要增加其他运算,只需要创建一个class 实现 interface,在getResult进行代码编写,最后在枚举中添加相关的造作符枚举(主要用于限制用户输入参数);
上一篇下一篇

猜你喜欢

热点阅读