Пример #1
0
        static void Main(string[] args)
        {
            IFactoryOp facAdd = new AddFactory();
            Operation  optAdd = facAdd.CreateOperate();

            optAdd.NumberA = 2;
            optAdd.NumberB = 3;
            optAdd.GetResult();
        }
        static void Main(string[] args)
        {
            IFactory factory = new AddFactory();

            OperationSuper operation = factory.CreateOperation();
            int            r         = operation.GetResult(123, 345);


            Console.WriteLine("计算结果是:" + r);
        }
Пример #3
0
        static void Main(string[] args)
        {
            IFactory  operFactory = new AddFactory();
            Operation oper        = operFactory.CreateOperation();

            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();

            Console.WriteLine(result);
            Console.ReadKey();
        }
        private static void NewMethod()
        {
            IFactory  operFactory = new AddFactory();
            Operation oper        = operFactory.CreateOperation();

            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();

            Console.WriteLine(result);
            Console.Read();
        }
Пример #5
0
        static void Main(string[] args)
        {
            //工厂方法里,客户端需要知道实例化哪个工厂,由工厂来具体生产对象。
            var a          = 100;
            var b          = 200;
            var type       = "+";
            var calculator = new AddFactory().GetCalculator();
            var result     = calculator?.Calculate(a, b);

            Console.WriteLine($"a{type}b={result}");

            Console.ReadKey();
        }
Пример #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("---简单计算器---");
            Console.Write("请输入左值:");
            string numLeftStr = Console.ReadLine();

            Console.WriteLine();
            Console.Write("请输入运算符:");
            string oper = Console.ReadLine();

            Console.WriteLine();
            Console.Write("请输入右值:");
            string numRightStr = Console.ReadLine();
            double numLeft = 0, numRight = 0;

            double.TryParse(numLeftStr, out numLeft);
            double.TryParse(numRightStr, out numRight);
            IFactory operFactory;

            switch (oper)
            {
            case "+":
                operFactory = new AddFactory();
                break;

            case "-":
                operFactory = new SubFactory();
                break;

            case "*":
                operFactory = new MulFactory();
                break;

            case "/":
                operFactory = new DivFactory();
                break;

            default:
                operFactory = new AddFactory();
                break;
            }
            Operate op = operFactory.CreateOperate();

            op.NumLeft  = numLeft;
            op.NumRight = numRight;
            Console.WriteLine("结果为{0}", op.GetResult());
        }
Пример #7
0
        static void Main(string[] args)
        {
            #region 计算器部分
            IFactory  operFactory = new AddFactory();
            Operation oper        = operFactory.CreateOperation();
            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();

            Console.WriteLine(result);
            #endregion

            #region 基本方式:薛磊风代表大学生学习雷锋
            LeiFeng xueleifeng = new Undergraduate();

            xueleifeng.BuyRice();
            xueleifeng.Sweep();
            xueleifeng.Wash();

            LeiFeng student1 = new Undergraduate();
            student1.BuyRice();
            LeiFeng student2 = new Undergraduate();
            student2.Sweep();
            LeiFeng student3 = new Undergraduate();
            student3.Wash();
            #endregion

            #region 简单工厂模式
            LeiFeng studentA = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentA.BuyRice();
            LeiFeng studentB = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentB.Sweep();
            LeiFeng studentC = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentC.Wash();
            #endregion

            #region 工厂方法模式
            ILeiFengFactory factory = new UndergraduateFactory();
            LeiFeng         student = factory.CreateLeiFeng();

            student.BuyRice();
            student.Sweep();
            student.Wash();

            Console.Read();
            #endregion
        }
Пример #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("请输入第一个数:");
            double dNum1 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("请输入第二个数:");
            double dNum2 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("请输入操作符:");
            string  strOp   = Console.ReadLine();
            Factory factory = null;

            switch (strOp)
            {
            case "+":
                factory = new AddFactory();
                break;

            case "-":
                factory = new SubFactory();
                break;

            case "*":
                factory = new MulFactory();
                break;

            case "/":
                factory = new DivFactory();
                break;

            case "%":
                factory = new ModuloFactory();
                break;

            default:
                Console.WriteLine("没有此工厂方法");
                factory = null;
                break;
            }
            if (factory != null)
            {
                Operation op = factory.CreateOperation();
                Console.WriteLine("{0}{1}{2}={3}", dNum1, strOp, dNum2, op.GetResult(dNum1, dNum2));
            }
        }
Пример #9
0
        static void Main(string[] args)
        {
            Console.WriteLine("---简单计算器---");
            Console.Write("请输入左值:");
            string numLeftStr = Console.ReadLine();
            Console.WriteLine();
            Console.Write("请输入运算符:");
            string oper = Console.ReadLine();
            Console.WriteLine();
            Console.Write("请输入右值:");
            string numRightStr = Console.ReadLine();
            double numLeft = 0, numRight = 0;
            double.TryParse(numLeftStr, out numLeft);
            double.TryParse(numRightStr, out numRight);
            IFactory operFactory;
            switch (oper)
            {
                case "+":
                    operFactory = new AddFactory();
                    break;
                case "-":
                    operFactory = new SubFactory();
                    break;
                case "*":
                    operFactory = new MulFactory();
                    break;
                case "/":
                    operFactory = new DivFactory();
                    break;
                default:
                    operFactory = new AddFactory();
                    break;

            }
            Operate op = operFactory.CreateOperate();
            op.NumLeft = numLeft;
            op.NumRight = numRight;
            Console.WriteLine("结果为{0}", op.GetResult());
        }