• C# 设计模式(22)策略模式


    策略模式

    代码实现:

    计算服务类:

    namespace StrategyPattern.Service
    {
        public interface ICalculation
        {
           public int Calculate(int data1,int data2);
        }
        public class Plus:ICalculation
        {
            public int Calculate(int data1, int data2)
            {
                int ret= data1 + data2;
                Console.WriteLine($"{data1}+{data2}={ret}");
                return ret;
            }
        }
        public class Subtract : ICalculation
        {
            public int Calculate(int data1, int data2)
            {
                int ret = data1 - data2;
                Console.WriteLine($"{data1}-{data2}={ret}");
                return ret;
            }
        }
        public class Multiply : ICalculation
        {
            public int Calculate(int data1, int data2)
            {
                int ret = data1 * data2;
                Console.WriteLine($"{data1}*{data2}={ret}");
                return ret;
            }
        }
        public class Division:ICalculation
        {
            public int Calculate(int data1, int data2)
            {
                int ret = data1 / data2;
                Console.WriteLine($"{data1}/{data2}={ret}");
                return ret;
            }
        }
    }

    计算上下文:

        public class CalculateContext
        {
            private readonly ICalculation _calculation;
            private readonly int _data1;
            private readonly int _data2;
    
            public CalculateContext(ICalculation calculation, int data1,int data2)
            {
                _calculation = calculation;
                _data1 = data1;
                _data2 = data2;
            }
    
            public void DoAction()
            {
                _calculation.Calculate(_data1, _data2);
            }
        }

    代码调用:

        class Program
        {
            static void Main(string[] args)
            {
                int data1, data2;
                Console.WriteLine("整数二元运算...");
                Console.WriteLine("请输入第一个整数:");
                while (!int.TryParse(Console.ReadLine(), out data1))
                {
                    Console.WriteLine("请重新输入第一个整数:");
                }
    
                Console.WriteLine("请选择运算符:+ - * /");
                string[] ops =new string[] {"+","-","*","/"};
                string op="";
                op= Console.ReadLine();
                while (!ops.Contains(op))
                {
                    Console.WriteLine("请重新输入运算符:+ - * / ");
                    op = Console.ReadLine();
                }
    
                Console.WriteLine("请输入第二个整数:");
                while (!int.TryParse(Console.ReadLine(), out data2))
                {
                    Console.WriteLine("请重新输入第二个整数:");
                }
    
                ICalculation calculation = null;
                if (op == "+")
                {
                     calculation = new Plus();
                }
                else if (op == "-")
                {
                    calculation = new Subtract();
                }
                else if (op == "*")
                {
                    calculation = new Multiply();
                }
                else if (op == "/")
                {
                    calculation = new Division();
                }
                CalculateContext calculateContext = new CalculateContext(calculation,data1, data2);
                calculateContext.DoAction();
            }
        }

    结果:

  • 相关阅读:
    codeforces 938 C. Constructing Tests
    codeforces 981 C.Useful Decomposition
    Wannafly 挑战赛16 A 取石子
    codeforces 873 D. Merge Sort(分治)
    lightoj 1158
    lightoj 1226
    lightoj 1382
    lightoj 1283
    hdu 5445 Food Problem (多重背包)
    light 1205
  • 原文地址:https://www.cnblogs.com/YourDirection/p/14103255.html
Copyright © 2020-2023  润新知