コード例 #1
0
        public void Strategy_CanSwitchStrategyAtRuntime()
        {
            //Strategy Pattern
            //Test that strategy can be swapped at runtime

            //Arrange
            //var numbers = new List<int>() { 1, 2, 3, 4, 5 };
            var strat    = new ConcreteStrategyGetMax();
            var newStrat = new ConcreteStrategyGetMin();
            var context  = new Context(strat);

            //Act
            context.SetCurrentStrategy(newStrat);
            var result = context.GetCurrentStrategy().GetType();

            Assert.IsTrue(newStrat.GetType().Name != result.GetType().Name);
        }
コード例 #2
0
        public void Strategy_MinStrategy_GetsMinNumber()
        {
            //Strategy Pattern
            //Test that one concrete strategy is working

            //Arrange
            var numbers = new List <int>()
            {
                1, 2, 3, 4, 5
            };
            var strat   = new ConcreteStrategyGetMin();
            var context = new Context(strat);

            //Act
            var result = context.SomeMethodUsingTheStrategy(numbers);

            Assert.IsTrue(numbers.Min() == result);
        }