Beispiel #1
0
        public static IStrategy FindStrategy(char @operator)
        {
            IStrategy strategyToChange = null;

            switch (@operator)
            {
            case '+':
                strategyToChange = new AdditionStrategy();
                break;

            case '-':
                strategyToChange = new SubtractionStrategy();
                break;

            case '*':
                strategyToChange = new MultiplicationStrategy();
                break;

            case '/':
                strategyToChange = new DivisionStrategy();
                break;

            default:
                throw new ArgumentException($"Invalid operator {@operator} !");
            }

            return(strategyToChange);
        }
 public PrimitiveCalculator()
 {
     this.additionStrategy       = new AdditionStrategy();
     this.subtractionStrategy    = new SubtractionStrategy();
     this.multiplicationStrategy = new MultiplicationStrategy();
     this.divisionStrategy       = new DivisionStrategy();
     this.isAddition             = true;
 }
Beispiel #3
0
        static void Main()                                                                    // 100/100 - Пример за DependencyInversion с ctor Injection!!!
        {
            PrimitiveCalculator calculator = new PrimitiveCalculator(new AdditionStrategy()); // default strategy!

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] tokens  = input.Split();
                string   command = tokens[0];

                if (command == "mode")
                {
                    char @operator = tokens[1][0];

                    ICalculationStrategy strategy = null;

                    switch (@operator)
                    {
                    case '+':
                        strategy = new AdditionStrategy();
                        break;

                    case '-':
                        strategy = new SubtractionStrategy();
                        break;

                    case '*':
                        strategy = new MultyplicationStrategy();
                        break;

                    case '/':
                        strategy = new DivisionStrategy();
                        break;
                    }

                    if (strategy == null)
                    {
                        throw new ArgumentException("Inavalid mode!");
                    }

                    calculator.ChangeStrategy(strategy);
                }
                else
                {
                    int firstOperand  = int.Parse(tokens[0]);
                    int secondOperand = int.Parse(tokens[1]);

                    int result = calculator.PerformCalculation(firstOperand, secondOperand);

                    Console.WriteLine(result);
                }
            }
        }