public static string Processor(IEnumerable <Animal> animals)
        {
            var stringBuilder = new StringBuilder();
            // The other part of the processor code constructs the actual chain.
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();
            var cat      = new CatHandler();

            // The client should be able to send a request to any handler, not
            // just the first one in the chain.
            monkey.SetNext(squirrel).SetNext(dog).SetNext(cat);
            if (animals == null)
            {
                return(stringBuilder.ToString());
            }
            foreach (var animal in animals)
            {
                stringBuilder.Append($"Client: Who wants a {animal.Food}?");
                var result = monkey.Handle(animal);
                stringBuilder.Append(result == null ? $"   {animal.Food} was left untouched.\n" : $"   {result}");
            }

            return(stringBuilder.ToString());
        }
        public void ChainOfResponsibilityTest()
        {
            // The other part of the client code constructs the actual chain.
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();

            monkey.SetNext(squirrel).SetNext(dog);

            // The client should be able to send a request to any handler, not just the first one in the chain.
            var monkeyResult = Client.ClientCode(monkey);
            var enumerable   = monkeyResult as string?[] ?? monkeyResult.ToArray();

            enumerable.Count().Should().Be(3);
            enumerable[0].Should().Be(BehavioralText.Squirrel(BehavioralText.Nut));
            enumerable[1].Should().Be(BehavioralText.Monkey(BehavioralText.Banana));
            enumerable[2].Should().Be(BehavioralText.Untouched(BehavioralText.Cheese));

            var squirrelResult = Client.ClientCode(squirrel);

            enumerable = squirrelResult as string?[] ?? squirrelResult.ToArray();
            enumerable.Count().Should().Be(3);
            enumerable[0].Should().Be(BehavioralText.Squirrel(BehavioralText.Nut));
            enumerable[1].Should().Be(BehavioralText.Untouched(BehavioralText.Banana));
            enumerable[2].Should().Be(BehavioralText.Untouched(BehavioralText.Cheese));
        }
Example #3
0
        private static void DoChainOfResponsibility()
        {
            var monkey   = new MonkeyHandler();
            var squirell = new SquirellHandler();
            var dog      = new DogHandler();

            monkey.SetNext(squirell).SetNext(dog);

            Console.WriteLine("Chain: Monkey > Squirell > Dog\n");
            BehavioralPatterns.ChainOfResponsibility.Client.ClientCode(monkey);
            Console.WriteLine(squirell);
        }
        public void MonkeyHandlerReturnExpected()
        {
            //Arrange
            var          monkeyHandler = new MonkeyHandler();
            const string expected      = "Monkey: I'll eat the BANANA.\n";
            //Act
            var result = monkeyHandler.Handle(new Animal {
                Food = "BANANA", Specie = "Monkey"
            }).ToString();

            //Assert
            Assert.Equal(expected, result);
        }
Example #5
0
        public void monkey_squirrel_dog()
        {
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();

            // Here the chain is created
            monkey.SetNext(squirrel).SetNext(dog);

            // Send one food at the time through the chain
            var result = Client.ClientCode(monkey, new[] { "Nut", "Banana", "Cup of coffee" });

            CollectionAssert.AreEqual(
                new[] {
                "Client: Who wants a Nut?",
                "Squirrel: I'll eat the Nut",

                "Client: Who wants a Banana?",
                "Monkey: I'll eat the Banana",

                "Client: Who wants a Cup of coffee?",
                "Cup of coffee was left untouched"
            },
                result);

            // Send a meatball through the chain

            var result2 = Client.ClientCode(monkey, new[] { "MeatBall" });

            CollectionAssert.AreEqual(
                new[] {
                "Client: Who wants a MeatBall?",
                "Dog: I'll eat the MeatBall",
            },
                result2);

            // Client can send a request to the middle of the chain
            // Here a banana is sent to the chain, but after the monkey, so no-one will handle it


            var result3 = Client.ClientCode(squirrel, new[] { "Banana" });

            CollectionAssert.AreEqual(
                new[] {
                "Client: Who wants a Banana?",
                "Banana was left untouched",
            },
                result3);
        }
        static void Main(string[] args)
        {
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();

            monkey.setNext(squirrel).setNext(dog);

            Console.WriteLine("Chain: Monkey > Squirerel > Dog\n");
            Client.ClientCode(monkey);
            Console.WriteLine();

            Console.WriteLine("Subchain: Squirrel > Dog\n");
            Client.ClientCode(squirrel);
        }
Example #7
0
        static void RunChain()
        {
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();

            monkey.SetNext(squirrel).SetNext(dog);

            Console.WriteLine("Chain: Monkey > Squirrel > Dog\n");
            ChainOfResponsibility.Client.ClientCode(monkey);
            Console.WriteLine();

            Console.WriteLine("Subchain: Squirrel > Dog\n");
            ChainOfResponsibility.Client.ClientCode(squirrel);
        }
        void CanChooseProperHandler()
        {
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();

            monkey.SetNext(squirrel).SetNext(dog);

            // The client should be able to send a request to any handler, not
            // just the first one in the chain.
            Print("Chain: Monkey > Squirrel > Dog\n");
            Client.ClientCode(monkey);
            Print("Subchain: Squirrel > Dog\n");
            Client.ClientCode(squirrel);
        }
        public void DisplayExample()
        {
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();

            monkey.SetNext(squirrel).SetNext(dog);
            squirrel.SetNext(dog);

            Console.WriteLine("Chain: Monkey > Squirrel > Dog");
            OfferFood(FoodType.Banana, monkey);
            OfferFood(FoodType.Nut, monkey);
            OfferFood(FoodType.Meatball, monkey);
            OfferFood(FoodType.Coffee, monkey);

            Console.WriteLine("\nChain: Squirrel > Dog");
            OfferFood("Banana", squirrel);
        }
Example #10
0
        static void Main(string[] args)
        {
            // The other part of the client code constructs the actual chain.
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();

            monkey.SetNext(squirrel).SetNext(dog);

            // The client should be able to send a request to any handler, not
            // just the first one in the chain.
            Console.WriteLine("Chain: Monkey > Squirrel > Dog\n");
            ClientCode.ClientCoRCode(monkey);
            Console.WriteLine();

            Console.WriteLine("Subchain: Squirrel > Dog\n");
            ClientCode.ClientCoRCode(squirrel);
        }
Example #11
0
        static void Main(string[] args)
        {
            // Другая часть клиентского кода создает саму цепочку.
            var monkey   = new MonkeyHandler();
            var squirrel = new SquirrelHandler();
            var dog      = new DogHandler();

            monkey.SetNext(squirrel).SetNext(dog);

            // Клиент должен иметь возможность отправлять запрос любому
            // обработчику, а не только первому в цепочке.
            Console.WriteLine("Chain: Monkey > Squirrel > Dog\n");
            ChainOfResponsibilityClient.ClientCode(monkey);
            Console.WriteLine();

            Console.WriteLine("Subchain: Squirrel > Dog\n");
            ChainOfResponsibilityClient.ClientCode(squirrel);

            Console.ReadKey();
        }