Esempio n. 1
0
        static void Main(string[] args)
        {
            var apple = new Product("Apple", Color.Red, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Blue, Size.Large);


            Product[] products = new[] { apple, tree, house };
            var       pf       = new ProductFilter();

            Console.WriteLine("Green products (old)");
            foreach (var p in pf.FilterBySize(products, Size.Large))
            {
                Console.WriteLine($"- {p.Name} is is {Size.Large} ");
            }

            var bf = new  BetterFilter();

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

            Console.WriteLine("Large Blue items");
            foreach (var p in bf.Filter(products,
                                        new AndSpecification <Product>(new ColorSpecification(Color.Blue),
                                                                       new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($"- {p.Name} is big and blue");
            }
            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var apple = new Product("Apple", Color.Green, Size.Small); // creating new product 💫

            var tree = new Product("Tree", Color.Green, Size.Large);

            var house = new Product("House", Color.Blue, Size.Huge);

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

            var productFilter = new ProductFilter();

            Console.WriteLine($"Green products (old): ");

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

            // now using better filter
            var betterFilter = new BetterFilter();

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

            Console.WriteLine("Large Blue Items : ");
            foreach (var p in betterFilter.Filter(products,
                                                  new AddSpecification <Product>(new ColorSpecification(Color.Blue), new SizeFilter(Size.Huge))))
            {
                Console.WriteLine($" - {p.Name} is big & blue.");
            }

            Console.ReadKey();
        }