zoukankan      html  css  js  c++  java
  • 设计模式之简单工厂模式

    UML图

    namespace Study2014.CjModelDesign
    {
        /// <summary>
        /// 2014-5-19
        /// 简单封装 
        /// 过程式开发?
        /// </summary>
        public class Operation
        {
            public static double GetOldResult(double numberA, double numberB, string operate)
            {
                double result = 0;
                switch (operate)
                { 
                    case "+":
                        result = numberA + numberB;
                        break;
                    case "-":
                        result = numberA - numberB;
                        break;
                    case "*":
                        result = numberA * numberB;
                        break;
                    case "/":
                        if (numberB == 0)
                        {
                            throw new Exception("被除数不能为0");
                        }
                        result = numberA / numberB;
                        break;
                }
                return result;
            }
    
            //另一种写法
            public double NumberA
            {
                get;
                set;
            }
    
            public double NumberB
            {
                get;
                set;
            }
    
            public virtual double GetResult()
            {
                return 0;
            }
        }
    }
    View Code
    namespace Study2014.CjModelDesign
    {
        /// <summary>
        /// +操作 简单工厂
        /// </summary>
        public class OperationAdd:Operation
        {
            public override double GetResult()
            {
                return this.NumberA + this.NumberB;
            }
        }
    }
    View Code
    namespace Study2014.CjModelDesign
    {
        /// <summary>
        /// 除 操作 简单工厂
        /// </summary>
        public class OperationDiv:Operation
        {
            public override double GetResult()
            {
                if (this.NumberB == 0)
                {
                    throw new Exception("除数不能为0");
                }
                return this.NumberA / this.NumberB;
    
            }
        }
    }
    View Code
     public class OperationFactory
        {
            /// <summary>
            /// 简单工厂模式
            ///
            /// </summary>
            /// <param name="operate"></param>
            /// <returns></returns>
            public static Operation CreateOperation(string operate)
            {
                //多态
                Operation op = null;
                switch (operate)
                { 
                    case "+":
                        op = new OperationAdd();
                        break;
                    case "-":
                        op = new OperationSub();
                        break;
                    case "*":
                        op = new OperationMul();
                        break;
                    case "/":
                        op = new OperationDiv(); 
                        break;
                }
                return op;
            }
        }
    View Code

    总结一下这样写的好处

    1. 松耦合,后续增加其他的业务类(如:平方等),只需增加一个新的子类(从Operation继承),再修改工厂的代码(加个分支).
    2. 其他的没想到...
  • 相关阅读:
    TP5学习笔记- 使用命令行创建控制器
    centos 7 下安装mysql5.7
    webserver的安装
    linux常用命令 服务器硬件资源信息
    SSH 安装/ config 配置以及免密码登录
    thinkphp ,laravel,yii2运行环境搭建.
    分享几个博客园代码样式的CSS配置(复制黏贴即可)
    vue中通过.sync修饰符实现子组件修改父组件数据
    vue中$attrs和$listeners以及inheritAttrs的用法
    Vue项目中实现用户登录及token验证
  • 原文地址:https://www.cnblogs.com/sportdog/p/3745239.html
Copyright © 2011-2022 走看看