• 简单工厂模式


    /*
     * 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
  • 相关阅读:
    python可视化---axvspan()函数
    python可视化---axhline()函数
    Git 操作
    miui10 傻瓜式安装google框架方法
    python 制作一对一聊天
    Pyqt5+python+ErIC6+QT designer
    session 详细解析(转)
    #Week7 Neural Networks : Learning
    Multilayer Perceptron
    Advice for applying ML & ML System Design
  • 原文地址:https://www.cnblogs.com/zhiaiyaya/p/3841738.html
Copyright © 2020-2023  润新知