示例#1
0
        // The client code is usually suited to work with a single handler. In
        // most cases, it is not even aware that the handler is part of a chain.
        public static void ClientCode(AbstractHandler handler)
        {
            foreach (var food in new List <string> {
                "Nut", "Banana", "Cup of coffee"
            })
            {
                Console.WriteLine($"Client: Who wants a {food}?");

                var result = handler.Handle(food);

                if (result != null)
                {
                    Console.Write($"   {result}");
                }
                else
                {
                    Console.WriteLine($"   {food} was left untouched.");
                }
            }
        }
示例#2
0
            // The client code is usually suited to work with a single handler. In
            // most cases, it is not even aware that the handler is part of a chain.
            public static List <string> ClientCode(AbstractHandler handler, IEnumerable <string> foodlist)
            {
                var events = new List <string>();

                foreach (var food in foodlist)
                {
                    events.Add($"Client: Who wants a {food}?");

                    var result = handler.Handle(food);

                    if (result != null)
                    {
                        events.Add(result.ToString());
                    }
                    else
                    {
                        events.Add($"{food} was left untouched");
                    }
                }
                return(events);
            }
示例#3
0
 public void Change(string c)
 {
     handler.Handle(c);
 }
示例#4
0
        public static void ContextCode(AbstractHandler handler)
        {
            // foreach (var Cup in new List<string> { "Latte", "Espresso", "BlackCoffee" })
            //{
            // Ask the user to choose an operator.
            Console.WriteLine("Select your coffee from the list below :");
            Console.WriteLine("\t 1 - Latte");
            Console.WriteLine("\t 2 - Espresso");
            Console.WriteLine("\t 3 - BlackCoffee");
            string Cup = Console.ReadLine();

            switch (Cup)
            {
            case "1":
            {
                foreach (var Ingredient  in new List <string> {
                        "Milk", "Sugar", "Coffee"
                    })
                {
                    Console.WriteLine($" is there {Ingredient}?");
                    var result = handler.Handle(Ingredient);

                    if (result != null)
                    {
                        Console.Write($"   {result}");
                    }
                    else
                    {
                        Console.WriteLine($" {Ingredient} was left untouched.");
                    }
                }
                Console.WriteLine($" Your cup of {Cup} is ready ----------------- ");
                break;
            }

//-------------------------------------------------------------------------------------------------------
            case "2":
            {
                foreach (var Ingredient  in new List <string> {
                        "Sugar", "Coffee"
                    })
                {
                    Console.WriteLine($" is there {Ingredient}?");
                    var result = handler.Handle(Ingredient);

                    if (result != null)
                    {
                        Console.Write($"   {result}");
                    }
                    else
                    {
                        Console.WriteLine($" {Ingredient} was left untouched.");
                    }
                }
                Console.WriteLine($" Your cup of {Cup} is ready ----------------- ");
                break;
            }

            //----------------------------------------------------
            case "3":
            {
                foreach (var Ingredient  in new List <string> {
                        "Water", "Sugar", "Coffee"
                    })
                {
                    Console.WriteLine($" is there {Ingredient}?");
                    var result = handler.Handle(Ingredient);

                    if (result != null)
                    {
                        Console.Write($"   {result}");
                    }
                    else
                    {
                        Console.WriteLine($" {Ingredient} was left untouched.");
                    }
                }
                Console.WriteLine($" Your cup of {Cup} is ready ----------------- ");
                break;
            }

            default:
                break;
            }
        }
示例#5
0
 public static ArgumentDataModel Execute(string[] args, AbstractHandler handler)
 {
     return(handler.Handle(args));
 }
示例#6
0
        public static Book ClientCode(AbstractHandler handler, string lang)
        {
            var tempList = new[]
            {
                new Autor
                {
                    Id        = 1,
                    FirstName = "Kamil",
                    LastName  = "Pajak",
                    Books     = new[]
                    {
                        new Book
                        {
                            Title            = "test2",
                            ShortDescription = "dunski opis",
                            LanguageCode     = "da-DK"
                        },

                        new Book
                        {
                            Title            = "test4",
                            ShortDescription = "Norweski ospi",
                            LanguageCode     = "nb-NO"
                        },
                        //   new Book
                        //{
                        //    Title="test1",
                        //    ShortDescription="angielski opis",
                        //    LanguageCode="en-GB"
                        //}
                    }
                },
                new Autor
                {
                    Id        = 2,
                    FirstName = "bartosz",
                    LastName  = "jarosz",
                    Books     = new[]
                    {
                        new Book
                        {
                            Title            = "test5",
                            ShortDescription = "portugalski opisz",
                            LanguageCode     = "pt-BR"
                        },
                        new Book
                        {
                            Title            = "test6",
                            ShortDescription = "Norweski ospi",
                            LanguageCode     = "nb-NO"
                        },
                        new Book
                        {
                            Title            = "test7",
                            ShortDescription = "angielski opis",
                            LanguageCode     = "en-GB"
                        }
                    }
                },
                new Autor
                {
                    Id        = 3,
                    FirstName = "tt",
                    LastName  = "tt",
                    Books     = new[]
                    {
                        new Book
                        {
                            Title            = "test8",
                            ShortDescription = "portugalski opisz",
                            LanguageCode     = "pt-BR"
                        },
                        new Book
                        {
                            Title            = "test9",
                            ShortDescription = "Norweski ospi",
                            LanguageCode     = "nb-NO"
                        },
                        // new Book
                        //{
                        //    Title="test3",
                        //    ShortDescription="polski opisz",
                        //    LanguageCode="pl-PL"
                        //}
                    }
                },
            };

            foreach (var item in tempList)
            {
                foreach (var bookTemp in item.Books)
                {
                    var result = handler.Handle(bookTemp.LanguageCode, lang);

                    if (result != null)
                    {
                        return(bookTemp);
                    }
                }
            }

            return(tempList[0].Books.ElementAt(0));
        }