大话设计模式之1简单工厂

用简单工厂实现 控制台跟窗体 简易计 算器 代码如下 

  public class Operation
    {
        private double _numberA = 0;
        private double _numberB = 0;

        public double NumberA
        {
            get { return _numberA; }
            set { _numberA = value; }
        }

        public double NumberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }

        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }


        /// <summary>
        /// 检查输入的字符串是否准确
        /// </summary>
        /// <param name="currentNumber"></param>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string checkNumberInput(string currentNumber, string inputString)
        {
            string result = "";
            if (inputString == ".")
            {
                if (currentNumber.IndexOf(".") < 0)
                {
                    if (currentNumber.Length == 0)
                    {
                        result = "0" + inputString;
                    }
                    else
                    {
                        result = currentNumber + inputString;
                    }


                }

            }
            else if (currentNumber == "0")
            {
                result = inputString;
            }
            else
            {
                result = currentNumber + inputString;
            }


            return result;
        }

    }

    /// <summary>
    /// 加法类
    /// </summary>

    public class OperationAdd : Operation
    {
        public override double GetResult()
        {
            return NumberA + NumberB;
        }
    }

    /// <summary>
    /// 减法类
    /// </summary>
    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }

    /// <summary>
    /// 平方根类
    /// </summary>
    public class OperationSqr : Operation
    {
        public override double GetResult()
        {
            return NumberA * NumberB;
        }
    }

    public class OperationSqrt : Operation
    {
        public override double GetResult()
        {
            if (NumberB < 0)
            {
                throw new Exception("负数不能平方根");
            }

            double result = 0;
            result = Math.Sqrt(NumberB);
            return result;

        }
    }

    /// <summary>
    /// 相反类
    /// </summary>
    public class OperationReverse : Operation
    {
        public override double GetResult()
        {
            return -NumberB;
        }
    }

    /// <summary>
    /// 乘法类
    /// </summary>
    class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }

    /// <summary>
    /// 除法类
    /// </summary>
    class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumberB == 0)
                throw new Exception("除数不能为0。");
            result = NumberA / NumberB;
            return result;
        }
    }



    /// <summary>
    /// 运算工厂
    /// </summary>
    public class OperationFactory
    {
        public static Operation CreateOperation(string operate)
        {
            Operation operation = null;
            switch (operate)
            {
                case "+":
                    operation = new OperationAdd();
                    break;
                case "-":
                    operation = new OperationSub();
                    break;
                case "*":
                    operation = new OperationMul();
                    break;
                case "/":
                    operation = new OperationDiv();
                    break;
                case "sqr":

                    operation = new OperationSqr();
                    break;

                case "sqrt":

                    operation = new OperationSqrt();
                    break;
                case "+/-":

                    operation = new OperationReverse();
                    break;


            }

            return operation;
        }
    }

完整代码 下载地址

https://download.csdn.net/download/mjkmjk485485/10777718

猜你喜欢

转载自blog.csdn.net/mjkmjk485485/article/details/83958280
今日推荐