大话设计模式-第1章 代码无错就是优?--简单工厂模式- 高飞网

第1章 代码无错就是优?--简单工厂模式

2016-02-01 11:39:29.0

定义

    简单工厂模式,只解决了简单的对象创建问题,如下图所示,运算类要求提供两个运算数,一个方法返回计算结果,使用简单工厂以后,可以根据运算符,创建不同的运算类,进而去计算。

下面是代码:


import java.io.*;
public class App{
    public static void main(String[] args)throws IOException{
        System.out.print("输入第一个数字:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String strNum1 = br.readLine();
        System.out.print("输入第二个数字:");
        BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
        String strNum2 = br2.readLine();
        System.out.print("输入操作符:");
        BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
        String op = br3.readLine();

        int num1 = Integer.parseInt(strNum1);
        int num2 = Integer.parseInt(strNum2);
        Operation oper = OperationFactory.createOperation(op);
        oper.setNum1(num1);
        oper.setNum2(num2);
        int result = oper.getResult();
        System.out.println(num1+op+num2+"="+result);


    }
}
class OperationFactory{
    public static Operation createOperation(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;
    }
}
abstract class Operation{
    private int num1;
    private int num2;

    public void setNum1(int num1){
        this.num1 = num1;
    }
    public int getNum1(){
        return this.num1;
    }
    public void setNum2(int num2){
        this.num2 = num2;
    }
    public int getNum2(){
        return this.num2;
    }
    public abstract int getResult();
}
class OperationAdd extends Operation{
    @Override
        public int getResult(){
            return getNum1() + getNum2();
        }
}

class OperationSub extends Operation{
    @Override
        public int getResult(){
            return getNum1() - getNum2();
        }
}

class OperationMul extends Operation{
    @Override
        public int getResult(){
            return getNum1() * getNum2();
        }
}

class OperationDiv extends Operation{
    @Override
        public int getResult(){
            try{
                if(getNum2()==0){
                    throw new Exception("除数不能为0");
                }
                return getNum1() / getNum2();
            }catch(Exception e){
            }
            return 0;
        }
}


应用

    下面是一个超市促销的场景,有正常收费、打折收费、返利等三种方式 ,利用简单工厂模式,可以封装实现类的创建细节:

下面是基本代码:


/**
*打折基类
*/
public abstract class CashSuper{
        public abstract double acceptCash(double money);       
}

/**
*正常收费
*/
public class CashNormal extends CashSuper{
        public double acceptCash(double money){
                return money;
        }
}

/**
* 打折收费
*/
public class CashRebate extends CashSuper{
        private double moneyRebate = 1d;//打折
        public CashRebate(double moneyRebate){
                this.moneyRebate = moneyRebate;
        }
        public double acceptCash(double money){
                return moneyRebate * money;
        }
}

/**
* 返利
*/
public class CashReturn extends CashSuper{
       
        private double moneyCondition = 0.0d;
        private double moneyReturn = 0.0d;

        public CashReturn(double moneyCondition,double moneyReturn){
                this.moneyCondition = moneyCondition;
                this.moneyReturn = moneyReturn;
        }
        public double acceptCash(double money){
                if(money >= moneyCondition){
                        money = money - (int)(money / moneyCondition)* moneyReturn;
                }
                return money;
        }
}

/**
* 简单工厂
*/
public class CashFactory{
        public static CashSuper createCashAccept(String type){
                CashSuper cash = null;
                switch(type){
                        case "正常收费":
                                cash = new CashNormal();
                        break;
                        case "满300返100":
                                cash = new CashReturn(300,100);
                        break;
                        case "打8折":
                                cash = new CashRebate(0.8d);
                        break;
                }
                return cash;
        }
}


总结

 简单工厂模式,只解决了对象创建问题,如果想要再添加实现类,工厂也需要跟着修改,这不复合开放封装原则。