• 设计模式学习笔记——简单工厂模式


    UML类图:

    Java版代码:

    abstract class Operation {
        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;
        }
    
        public abstract double GetResult();
    
    }
    
    class OperationAdd extends Operation {
        @Override
        public double GetResult() {
            // TODO Auto-generated method stub
            double result = 0;
            result = getNumberA() + getNumberB();
            return result;
        }
    }
    
    class OperationSub extends Operation{
        @Override
        public double GetResult() {
            // TODO Auto-generated method stub
            double result=0;
            result=getNumberA()-getNumberB();
            return result;
        }
    }
    
    class OperationMul extends Operation{
        @Override
        public double GetResult() {
            // TODO Auto-generated method stub
            double result=0;
            result=getNumberA()*getNumberB();
            return result;
        }
    }
    
    class operationDiv extends Operation{
        @Override
        public double GetResult() {
            // TODO Auto-generated method stub
            double result=0;
            if(getNumberB()==0){
                System.out.println("除数不能为零!");
            }else{
                result=getNumberA()/getNumberB();
            }
            return result;
        }
    }
    
    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;
        }
    }
    public class SimpleFactory{
        public static void main(String[] args){
            Operation oper;
            oper =OperationFactory.createOperate("+");
            oper.setNumberA(1);
            oper.setNumberB(2);
            double result=oper.GetResult();
            System.out.println(result);
        }
    }

    总结:

    这里的工厂类OperationFactory不是与具体的运算类相关联,而是与抽象运算类相关联,这样就降低了耦合度,即使要扩展或修改也不需要大范围的修改代码,

    增强了代码的可维护性和可扩展性。

    
    
  • 相关阅读:
    2017年7月10日 20:34:02 简易博客记录组
    Oracle审计表AUD$处理方法 (Z)
    Oracle中trace的几种(Z)
    查看oracle数据库是否归档和修改归档模式(转)
    oracle exp-000991
    基于公司级平台封装的SqlserverHelper
    pycharm运行测试程序提示no tests were found
    10-13 验证用户
    10-11 喜欢的数字
    10-1 Python 学习笔记
  • 原文地址:https://www.cnblogs.com/kcher90/p/2980267.html
Copyright © 2020-2023  润新知