Beispiel #1
0
        static void Main(string[] args)
        {
            var FC5             = new Games("Far Cry 5", GenreType.FirstPerson, WorldType.OpenWorld);
            var PUBG            = new Games("PUBG", GenreType.FirstPerson, WorldType.OpenWorld);
            var RainbowSixSiege = new Games("R6", GenreType.FirstPerson, WorldType.MissionBased);
            var ACOrigins       = new Games("Assassins Creed Origins", GenreType.ThirdPerson, WorldType.OpenWorld);

            Games[] products = { FC5, PUBG, RainbowSixSiege, ACOrigins };

            WriteLine("All games in this instance:");
            foreach (var g in products)
            {
                WriteLine($" - {g.Name}");
            }
            var pf = new ProductFilter();

            WriteLine("First person games products (Non OCP):");
            foreach (var p in pf.FilterByGenre(products, GenreType.FirstPerson))
            {
                WriteLine($" - {p.Name} is first person");
            }
            WriteLine("Third person games products (Non OCP):");
            foreach (var p in pf.FilterByGenre(products, GenreType.ThirdPerson))
            {
                WriteLine($" - {p.Name} is third person");
            }

            // ^^ BEFORE

            // vv AFTER
            WriteLine("");
            var bf = new BetterFilter();

            WriteLine("First person games (OCP):");
            foreach (var p in bf.Filter(products, new GenreSpecification(GenreType.FirstPerson)))
            {
                WriteLine($" - {p.Name} is first person");
            }
            WriteLine("Third person games (OCP):");
            foreach (var p in bf.Filter(products, new GenreSpecification(GenreType.ThirdPerson)))
            {
                WriteLine($" - {p.Name} is third person");
            }

            WriteLine("Open world games");
            foreach (var p in bf.Filter(products, new SizeSpecification(WorldType.OpenWorld)))
            {
                WriteLine($" - {p.Name} is open world");
            }

            WriteLine("Open world first person shooters");
            foreach (var p in bf.Filter(products, new AndSpecification <Games>(new GenreSpecification(GenreType.FirstPerson), new SizeSpecification(WorldType.OpenWorld)))
                     )
            {
                WriteLine($" - {p.Name} is open world first person.");
            }
        }
        static void Main(string[] args)
        {
            var apple = new Product("Apple", Color.Green, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Blue, Size.Large);

            Product[] products = { apple, tree, house };

            var pf = new ProductFilter();

            WriteLine("Green products (old):");
            foreach (var p in pf.FilterByColor(products, Color.Green))
            {
                WriteLine($" - {p.Name} is green");
            }

            // ^^ BEFORE

            // vv AFTER
            var bf = new BetterFilter();

            WriteLine("Green products (new):");
            foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                WriteLine($" - {p.Name} is green");
            }

            WriteLine("Large products");
            foreach (var p in bf.Filter(products, new SizeSpecification(Size.Large)))
            {
                WriteLine($" - {p.Name} is large");
            }

            var largeGreenSpec = new ColorSpecification(Color.Green)
                                 & new SizeSpecification(Size.Large);

            //var largeGreenSpec = Color.Green.And(Size.Large);

            WriteLine("Large blue items");
            foreach (var p in bf.Filter(products,
                                        new AndSpecification <Product>(new ColorSpecification(Color.Blue),
                                                                       new SizeSpecification(Size.Large)))
                     )
            {
                WriteLine($" - {p.Name} is big and blue");
            }
        }
        static void Main(string[] args)
        {
            var apple = new Product("Apple", Color.Green, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Blue, Size.Large);

            Product[] products = { apple, tree, house };

            var pf = new ProductFilter();

            WriteLine("Green products (old):");
            foreach (var p in pf.FilterByColor(products, Color.Green))
            {
                WriteLine($" - {p.Name} is green");
            }

            // ^^ BEFORE

            // vv AFTER
            //abstract actual enum into different func/obj then add different obj into the actual func instead of modifying the filter func
            var bf = new BetterFilter();

            WriteLine("Green products (new):");
            foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                WriteLine($" - {p.Name} is green");
            }

            WriteLine("Large products");
            foreach (var p in bf.Filter(products, new SizeSpecification(Size.Large)))
            {
                WriteLine($" - {p.Name} is large");
            }

            WriteLine("Large blue items");
            foreach (var p in bf.Filter(products,
                                        new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large)))
                     )
            {
                WriteLine($" - {p.Name} is big and blue");
            }
        }