コード例 #1
0
        /// <summary>
        /// I am using a client service design of dependency injection (method injection)
        /// Robot is my client and IChip interface is my service
        /// This way the robot does not need to know what's the logic behind a chip and it will accept and execute any chip
        /// I am using interface for Chip so that I can utilizate runtime polymorphism for swapping chips at runtime
        /// Created a seperate class for unit test so that I can add as many static test methods in it as I need
        /// </summary>

        static void Main(string[] args)
        {
            // Service interface
            IChip inputChip;

            // Run test cases
            UnitTestProvider.RunTestCases();

            // hardcoded numerical array input to demo functionalities
            //int[] input = new int[] { 4, 67, 2, 34, 5 };

            int[] input = null;

            // Client class type
            Robot robot = new Robot();

            /****************** Demo Case 1****************************/
            // Running robot's chip execution menthod without installing a chip
            object resultWithoutChip = robot.executeChipLogic(input);

            Type x0 = resultWithoutChip.GetType();

            DisplayResults(resultWithoutChip, x0);

            /****************** Demo Case 2****************************/
            // selecting and installing chip of sorting.
            inputChip = new ChipOfSorts();
            robot.installChip(inputChip);

            // Result object returned after chip logic execution. Robot does not know what was the logic behind the chip.
            object resultforChipOfSorts = robot.executeChipLogic(input);

            Type x1 = resultforChipOfSorts.GetType();

            DisplayResults(resultforChipOfSorts, x1);

            /****************** Demo Case 3****************************/
            // selecting and installing chip of sum
            // Swapping the chip using runtime polymorphism as different Chip can be passed that implement same IChip interface
            inputChip = new ChipOfSum();
            robot.installChip(inputChip);

            object resultforChipOfSum = robot.executeChipLogic(input);

            Type x2 = resultforChipOfSum.GetType();

            DisplayResults(resultforChipOfSum, x2);

            Console.ReadKey();
        }
コード例 #2
0
        public static bool TestMethodForChipOfSum()
        {
            bool  result = false;
            IChip chip   = new ChipOfSorts();

            object validSumResult = 32;

            chip = new ChipOfSum();

            try
            {
                if ((int)validSumResult == (int)chip.chipFunction(new int[] { 10, 8, 9, 5 }))
                {
                    result = true;
                }
            }
            catch (Exception e)
            {
                result = false;
            }
            return(result);
        }