public void ShouldCreateModeCalculator()
        {
            var averageCalculatorFactory = new AverageCalculatorFactory();

            var calculator = averageCalculatorFactory.CreateCalculator(CalculatorType.Mode);

            Assert.That(calculator, Is.TypeOf<Mode>());
        }
Пример #2
0
        public int CalculateAverage_TheGoodWay(int[] listOfValues, CalculatorType type)
        {
//          Responsibility of deciding which implementation to use is encapsulated within the Factory class
            var creatorOfAverageCalulators = new AverageCalculatorFactory();

            var averageCalculator = creatorOfAverageCalulators.CreateCalculator(type);

            //responsibility for performing the calculation is pushed down to where it belongs; with each of the concrete implementations of IAverageCalculator

            return(averageCalculator.Average(listOfValues));

            /*
             * Here we have separated Responsibilities of deciding which implementation to use, and the actual implementations of those calculations.
             * Now, if we want to change how we calculate Mode (the implementation is a bit poor and could use a refactor!):
             * - we have no fear of breaking the other calculations
             * - we have no fear of breaking the logic that determines which algorithm to use
             * - Each implementation is independently testable, as is the factory.
             * Also
             * - Its easy to add or remove implementations without the breaking the others
             */
        }
Пример #3
0
        public int CalculateAverage_TheGoodWay(int[] listOfValues, CalculatorType type)
        {
            //          Responsibility of deciding which implementation to use is encapsulated within the Factory class
            var creatorOfAverageCalulators = new AverageCalculatorFactory();

            var averageCalculator = creatorOfAverageCalulators.CreateCalculator(type);

            //responsibility for performing the calculation is pushed down to where it belongs; with each of the concrete implementations of IAverageCalculator

            return averageCalculator.Average(listOfValues);

            /*
             Here we have separated Responsibilities of deciding which implementation to use, and the actual implementations of those calculations.
             Now, if we want to change how we calculate Mode (the implementation is a bit poor and could use a refactor!):
             - we have no fear of breaking the other calculations
             - we have no fear of breaking the logic that determines which algorithm to use
             - Each implementation is independently testable, as is the factory.
             Also
             - Its easy to add or remove implementations without the breaking the others
             */
        }