예제 #1
0
        static void Main(string[] args)
        {
            var apple = new Product("Apple", Colour.Green, Size.Small);
            var tree  = new Product("Tree", Colour.Green, Size.Large);
            var house = new Product("House", Colour.Blue, Size.Large);

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

            WriteLine("Large products (old method):");

            var pf = new ProductFilter();

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

            WriteLine("Large products (new method):");
            var bf = new BetterFilter();

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

            // See how easy to add a new filter of colour.
            // The filter stays the same, add a new specification.
            // Open to extenstion by add a new specification,
            // close to modification to filter and specification.
            WriteLine("Green products (old method):");
            foreach (var p in pf.FilterByColour(products, Colour.Green))
            {
                WriteLine($" - {p.Name} is green");
            }

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

            // Add combination filter
            WriteLine("Large blue item:");
            foreach (var p in bf.Filter(products, new AndSpecification(
                                            new ColourSpecification(Colour.Blue),
                                            new SizeSpecification(Size.Large))))
            {
                WriteLine($" - {p.Name} is big and blue");
            }
        }
예제 #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       bf       = new BetterFilter();

            WriteLine("Green Products: ");
            foreach (var product in bf.Filter(products, new ColorSpeficiation(Color.Green)))
            {
                WriteLine($"- {product.Name} is Green");
            }
            WriteLine("Large Products: ");
            foreach (var product in bf.Filter(products, new IAndSpecification <Product>(new ColorSpeficiation(Color.Blue), new SizeSpeficiation(Size.Large))))
            {
                WriteLine($"- {product.Name} is Blue and Big");
            }
            ReadLine();
        }