首页 > 代码库 > 简单工厂模式

简单工厂模式

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package simpleFactory;/** * * @author XiaoTianCai *///Operation运算类public abstract class Operation {    private double _numberA = 0;    private double _numberB = 0;    public void setNumberA(double a) {        _numberA = a;    }    public void setNumberB(double b) {        _numberB = b;    }    public double getNumberA() {        return _numberA;    }    public double getNumberB() {        return _numberB;    }        public abstract double gerResult();}
Operation
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package simpleFactory;/** * * @author XiaoTianCai *///加法类class OperationAdd extends Operation {    @Override    public double gerResult() {        double result = 0;        result = getNumberA() + getNumberB();        return result;    }}
OperationAdd
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package simpleFactory;/** * * @author XiaoTianCai *///减法类class OperationSub extends Operation {    @Override    public double gerResult() {        double result = 0;        result = this.getNumberA() - this.getNumberB();        return result;    }}
OperationSub.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package simpleFactory;/** * * @author XiaoTianCai */class OperationMul extends Operation {    @Override    public double gerResult() {        double result = 0;        result = this.getNumberA() * this.getNumberB();        return result;    }}
OperationMul.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package simpleFactory;/** * * @author XiaoTianCai */class OperationDiv extends Operation {    @Override    public double gerResult() {        double result = 0;        try {            result = this.getNumberA() / this.getNumberB();        } catch (Exception ex) {            System.out.println("除数不能为0.");        }        return result;    }}
OperationDiv.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package simpleFactory;/** * * @author XiaoTianCai *///简单运算工厂类public class OperationFactory {    public static Operation createOperate(String operate)    {        Operation oper=null;        switch(operate)        {            case "+":                oper=new OperationAdd();                break;            case "-":                oper=new OperationSub();                break;            case "*":                oper=new OperationMul();                break;            case "/":                oper=new OperationDiv();                break;        }        return oper;    }   }
OperationFactory.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package simpleFactory;/** * * @author XiaoTianCai */public class mainTest {    public static void main(String[] args) {        Operation oper;        /*         只需要输入运算符号,工厂就实例化出合适的对象,听过多态,返回父类的方式实现计算结果         */        oper = OperationFactory.createOperate("/"); //实例化对象        oper.setNumberA(12);        oper.setNumberB(0);        double result = oper.gerResult();        System.out.println(result);    }}
mainTest .java

 

简单工厂模式