Пример #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a sentence");
            string input = Console.ReadLine();

            Action actionToCheckPalindrome = () =>
            {
                Container container = new Container();

                string palindromeStrategyValue = ConfigUtils.GetValueByKey("PalindromeStrategyType");
                PalindromeStrategyType palindromeStrategyType = StringUtils.ToEnum(palindromeStrategyValue, PalindromeStrategyType.Recursive);

                IPalindromeFactory  palindromeProcessor = container.GetInstance <IPalindromeFactory>();
                IPalindromeStrategy palindromeStrategy  = palindromeProcessor.GetPalindromeStrategy(palindromeStrategyType);
                bool isInputPalindrome = palindromeStrategy.CheckIfInputIsPalindrome(input);

                Console.WriteLine(string.Format("Your input is {0} Palindrome", (isInputPalindrome ? "a" : "not a")));
            };

            Action <Exception> actionToLogException = excpetion => Console.WriteLine(excpetion.Message);

            ExceptionHandler.TryCatch(actionToCheckPalindrome, actionToLogException);

            Console.ReadLine();
        }
Пример #2
0
        public void ReturnPalindromeStrategyIfStrategyTypeIsImplemented()
        {
            PalindromeStrategyType strategyType = PalindromeStrategyType.Recursive;
            IPalindromeStrategy strategy = factory.GetPalindromeStrategy(strategyType);

            Assert.IsType<RecursivePalindromeStrategy>(strategy);
        }
Пример #3
0
        public void ReturnDefualtEnumWhenStrategyTypeIsInvalid()
        {
            string strategyTypeValue = "Advanced";

            PalindromeStrategyType strategyType = StringUtils.ToEnum(strategyTypeValue, PalindromeStrategyType.None);

            Assert.Equal(PalindromeStrategyType.None, strategyType);
        }
Пример #4
0
        public void ReturnTargetEnumWhenStrategyTypeIsValid()
        {
            string strategyTypeValue = "Recursive";

            PalindromeStrategyType strategyType = StringUtils.ToEnum(strategyTypeValue, PalindromeStrategyType.None);

            Assert.Equal(PalindromeStrategyType.Recursive, strategyType);
        }
Пример #5
0
        // Summary:
        //     Create a strategy instance base on required strategy type.
        //
        // Parameters:
        //   strategyType:
        //     The enum represents the type of target strategy.
        //
        // Returns:
        //     The palindrome strategy instance.
        //
        // Exceptions:
        //   PalindromeStrategyNotImplementedException:
        //     When cannot initialize an instance meets target strategy type.
        public IPalindromeStrategy GetPalindromeStrategy(PalindromeStrategyType strategyType)
        {
            IPalindromeStrategy palindromeStrategy = palindromeStrategies.SingleOrDefault(p => p.StategyType == strategyType);

            if (palindromeStrategy == null)
            {
                throw new PalindromeStrategyNotImplementedException(strategyType.ToString());
            }

            return(palindromeStrategy);
        }
Пример #6
0
 public void ThrowExceptionIfStrategyTypeIsNotImplemented()
 {
     PalindromeStrategyType strategyType = PalindromeStrategyType.NotImplementedStrategyType;
     Assert.Throws<PalindromeStrategyNotImplementedException>(() => factory.GetPalindromeStrategy(strategyType));
 }