예제 #1
0
    static void Main(string[] args)
    {
        Operation opera = new Operation();

        opera         = SimpleFactory.CreateOperation("+");
        opera.NumberA = 5;
        opera.NumberB = 6;
        System.Console.Write(opera.NumberA + "+" + opera.NumberB + "=" + opera.getResult());
    }
예제 #2
0
        static void Main(string[] args)
        {
            /*
             * 面向过程方式实现计算器
             * 1、面向过程的编程
             * 2、添加运算类型时,改动较大
             * OCP 开放-封闭
             */
            string opt = Console.ReadLine();
            double a   = double.Parse(Console.ReadLine());
            double b   = double.Parse(Console.ReadLine());

            double res;

            switch (opt)
            {
            case "+":
                res = a + b;
                break;

            case "-":
                res = a - b;
                break;
            }


            /*
             * 面向对象的思想,通过简单工厂实现的计算器
             * 调用者角色,
             */
            Operation addOp = SimpleFactory.CreateOperation("+");

            if (null != addOp)
            {
                addOp.NumberA = double.Parse(Console.ReadLine());
                addOp.NumberB = double.Parse(Console.ReadLine());

                Console.WriteLine(addOp.GetResult());
            }
        }