static void Main(string[] args) { Product[] products = new Product[] { new Product("Apple", Color.Green, Size.Small), new Product("Tree", Color.Green, Size.Large), new Product("House", Color.Blue, Size.Large) }; var bf = new BetterFilter(); foreach (var p in bf.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Green), new SizeSpecification(Size.Large)))) { Console.WriteLine($"name : {p.Name}"); } foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($"name : {p.Name}"); } }
static void SOLIDOpenClosed() { 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"); } // ^^ BEFORE // vv AFTER var bf = new BetterFilter(); Console.WriteLine("Green products (new):"); foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($" - {p.Name} is green"); } Console.WriteLine("Large products"); foreach (var p in bf.Filter(products, new SizeSpecification(Size.Large))) { Console.WriteLine($" - {p.Name} is large"); } Console.WriteLine("Large blue items"); foreach (var p in bf.Filter(products, new BanchOfSpecifications <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))) ) { Console.WriteLine($" - {p.Name} is big and blue"); } }
public static void Test() { 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 = { apple, tree, house }; // INCORRECT IMPLEMENTATION /* * var pf = new ProductFilter(); * WriteLine("Green products (incorrect):"); * * foreach (var p in pf.FilterByColor(products, Color.GREEN)) { * WriteLine($" - {p.Name} is green"); * } */ // CORRECT IMPLEMENTATION var bf = new BetterFilter(); WriteLine("Green products (correct):"); foreach (var p in bf.Filter(products, new ColorSpecification(Color.GREEN))) { WriteLine($" - {p.Name} is green"); } 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(); 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 Prodcuts (new) : "); foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($"- {p.Name} is green"); } }