Example #1
0
        static void Main(string[] args)
        {
            var apple = new Product("Apple", Color.Green, Size.Small, Price.Cheap);
            var tree  = new Product("Tree", Color.Green, Size.Large, Price.Affordable);
            var house = new Product("House", Color.Blue, Size.Large, Price.Expensive);
            var car   = new Product("Car", Color.Red, Size.Large, Price.Expensive);


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

            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
            // using the better filter// filter takes array of products and color specification as green as argument.
            // loop over each product and return the Name of product that matches that specific color Green
            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");
            }

            // using the
            WriteLine("Large blue, expensive items");
            foreach (var p in bf.Filter(products,
                                        new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large), new PriceSpecification(Price.Expensive))))
            {
                WriteLine($" - {p.Name} is big, blue and expensive");
            }
        }
Example #2
0
        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();

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

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

            var bf = new BetterFilter();

            Console.WriteLine("Green product (new) :");
            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();
        }