Пример #1
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的策略模式");
                Console.WriteLine("下面是一个计算器展示demo:");
                while (true)
                {
                    Console.WriteLine("******************************");
                    Console.WriteLine("******************************");
                    Console.WriteLine("******************************");
                    #region 用户输入和校验  前端逻辑
                    int    iInputLeft  = 0;
                    int    iInputRight = 0;
                    string operate     = "";

                    Console.WriteLine("输入第一个数字(整数):");
                    string sInputLeft = Console.ReadLine();
                    if (!int.TryParse(sInputLeft, out iInputLeft))
                    {
                        Console.WriteLine("输入数字无效,请重新输入");
                        continue;
                    }

                    Console.WriteLine("输入计算符号(+-*/):");
                    operate = Console.ReadLine();
                    if (!new string[] { "+", "-", "*", "/" }.Contains(operate))
                    {
                        Console.WriteLine("输入计算符号无效,请重新输入");
                        continue;
                    }

                    Console.WriteLine("输入第二个数字(整数):");
                    string sInputRight = Console.ReadLine();
                    if (!int.TryParse(sInputRight, out iInputRight))
                    {
                        Console.WriteLine("输入数字无效,请重新输入");
                        continue;
                    }
                    #endregion

                    #region 具体计算 业务逻辑
                    int iResult = 0;
                    //switch (operate)
                    //{
                    //    case "+":
                    //        iResult = iInputLeft + iInputRight;
                    //        break;
                    //    case "-":
                    //        iResult = iInputLeft - iInputRight;
                    //        break;
                    //    case "*":
                    //        iResult = iInputLeft * iInputRight;
                    //        break;
                    //    case "/":
                    //        iResult = iInputLeft / iInputRight;//非0检测  每一个分支的逻辑可能很复杂
                    //        break;
                    //    default:
                    //        Console.WriteLine("输入符号异常,重新输入");
                    //        continue;
                    //}

                    //BaseCaculation baseCaculation = null;
                    //switch (operate)//就是把计算的逻辑封装到类里面,同时提供了抽象,好处就在于逻辑清晰,维护方便
                    //{
                    //    case "+":
                    //        baseCaculation = new Plus();
                    //        break;
                    //    case "-":
                    //        baseCaculation = new Minus();
                    //        break;
                    //    case "*":
                    //        baseCaculation = new Multiply();
                    //        break;
                    //    case "/":
                    //        baseCaculation = new Division();
                    //        break;
                    //    default:
                    //        Console.WriteLine("输入符号异常,重新输入");
                    //        continue;
                    //}
                    BaseCaculation baseCaculation = SimpleFactory.CreateCaculation(operate);

                    //iResult = baseCaculation.Cuculation(iInputLeft, iInputRight);
                    Context context = new Context(iInputLeft, iInputRight, baseCaculation);
                    iResult = context.Caculation();

                    Console.WriteLine("计算为: {0}{1}{2}={3}", iInputLeft, operate, iInputRight, iResult);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎来到软谋教育.net高级班公开课之设计模式特训课,今天是Eleven老师为大家带来的策略模式");
            bool isGoon = true;

            while (isGoon)
            {
                Console.WriteLine("请输入第一个数字:");
                string sInput1 = Console.ReadLine();
                Console.WriteLine("请输入+-*/:");
                string sOperate = Console.ReadLine();
                Console.WriteLine("请输入第二个数字:");
                string sInput2 = Console.ReadLine();
                int    iInput1 = 0;
                int    iInput2 = 0;
                if (!int.TryParse(sInput1, out iInput1) || !int.TryParse(sInput2, out iInput2))
                {
                    Console.WriteLine("请输入有效数字");
                    continue;
                }
                //检测0
                int result = 0;
                //switch (sOperate)//业务逻辑和算法混合在一起去了
                //{
                //    case "+":
                //        result = iInput1 + iInput2;
                //        break;
                //    case "-":
                //        result = iInput1 - iInput2;
                //        break;
                //    case "*":
                //        result = iInput1 * iInput2;
                //        break;
                //    case "/":
                //检测0
                //        result = iInput1 / iInput2;
                //        break;
                //    default:
                //        {
                //            Console.WriteLine("操作符号异常");
                //            continue;
                //        }
                //}


                //switch (sOperate)//业务逻辑和算法分开
                //{
                //    case "+":
                //        {
                //            BaseStrategy strategy = new PlusStrategy();
                //            strategy.LeftInput = iInput1;
                //            strategy.RightInput = iInput2;
                //            result = strategy.Calculation();
                //        }
                //        break;
                //    case "-":
                //        {
                //            BaseStrategy strategy = new MinusStrategy();
                //            strategy.LeftInput = iInput1;
                //            strategy.RightInput = iInput2;
                //            result = strategy.Calculation();
                //        }
                //        break;
                //    case "*":
                //        {
                //            BaseStrategy strategy = new MutiplyStrategy();
                //            strategy.LeftInput = iInput1;
                //            strategy.RightInput = iInput2;
                //            result = strategy.Calculation();
                //        }
                //        break;
                //    case "/":
                //        {
                //            BaseStrategy strategy = new DivisionStrategy();
                //            strategy.LeftInput = iInput1;
                //            strategy.RightInput = iInput2;
                //            result = strategy.Calculation();
                //        }
                //        break;
                //    default:
                //        {
                //            Console.WriteLine("操作符号异常");
                //            continue;
                //        }
                //}

                //switch (sOperate)//引入context  1 可以添加额外处理  2为计算准备一些客户端不需要知道的数据
                //{
                //    case "+":
                //        {
                //            BaseStrategy strategy = new PlusStrategy();
                //            Context context = new Context(strategy);
                //            result = context.Calculation(iInput1,iInput2);
                //        }
                //        break;
                //    case "-":
                //        {
                //            BaseStrategy strategy = new MinusStrategy();
                //            Context context = new Context(strategy);
                //            result = context.Calculation(iInput1, iInput2);
                //        }
                //        break;
                //    case "*":
                //        {
                //            BaseStrategy strategy = new MutiplyStrategy();
                //            Context context = new Context(strategy);
                //            result = context.Calculation(iInput1, iInput2);
                //        }
                //        break;
                //    case "/":
                //        {
                //            BaseStrategy strategy = new DivisionStrategy();
                //            Context context = new Context(strategy);
                //            result = context.Calculation(iInput1, iInput2);
                //        }
                //        break;
                //    default:
                //        {
                //            Console.WriteLine("操作符号异常");
                //            continue;
                //        }
                //}//上端必须知道所有的算法,并负责选择算法



                //不需要知道算法  也不负责选择   职责交给简单工厂
                BaseStrategy strategy = SimpleFactory.CreateInstance(sOperate);
                Context      context  = new Context(strategy);
                result = context.Calculation(iInput1, iInput2);


                Console.WriteLine("{0}{1}{2}={3}", iInput1, sOperate, iInput2, result);
            }


            Console.Read();
        }