码迷,mamicode.com
首页 > 其他好文 > 详细

简单工厂模式

时间:2014-09-18 14:45:44      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   java   ar   div   

bubuko.com,布布扣
/*
 * 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
bubuko.com,布布扣
/*
 * 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
bubuko.com,布布扣
/*
 * 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
bubuko.com,布布扣
/*
 * 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
bubuko.com,布布扣
/*
 * 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
bubuko.com,布布扣
/*
 * 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
bubuko.com,布布扣
/*
 * 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

 

简单工厂模式

标签:style   blog   http   color   io   os   java   ar   div   

原文地址:http://www.cnblogs.com/zhiaiyaya/p/3841738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!