static void Main(string[] args) { var apple = new Product("Apple", Enums.Color.Green, Enums.Size.Small); var tree = new Product("Tree", Enums.Color.Green, Enums.Size.Large); var house = new Product("House", Enums.Color.Blue, Enums.Size.Large); Product[] products = { apple, tree, house }; var pf = new ProductFilter(); Console.WriteLine("Green products (old):"); foreach (var p in pf.FilterByColor(products, Enums.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(Enums.Color.Green))) { Console.WriteLine($" - {p.Name} is green"); } Console.WriteLine("Large products"); foreach (var p in bf.Filter(products, new SizeSpecification(Enums.Size.Large))) { Console.WriteLine($" - {p.Name} is large"); } Console.WriteLine("Large blue items"); foreach (var p in bf.Filter(products, new AndSpecification <Product>(new ColorSpecification(Enums.Color.Blue), new SizeSpecification(Enums.Size.Large))) ) { Console.WriteLine($" - {p.Name} is big and blue"); } Console.ReadLine(); }
static void Main(string[] args) { var apple = new Product("Apple", Enums.Color.Green, Enums.Size.Small); var tree = new Product("Tree", Enums.Color.Green, Enums.Size.Large); var house = new Product("Houses", Enums.Color.Blue, Enums.Size.Large); Product[] products = { apple, tree, house }; var productFilter = new ProductFilter(); Console.WriteLine(@"Green products (OLD): "); foreach (var product in productFilter.FilterByColor(products, Enums.Color.Green)) { Console.WriteLine($"{product.Name} is green"); } var betterFilter = new BetterFilter(); Console.WriteLine(@"Green products (NEW): "); foreach (var product in betterFilter.Filter(products, new ColorSpecification(Enums.Color.Green))) { Console.WriteLine($"{product.Name} is green"); } Console.WriteLine(@"Green and Small products (NEW): "); foreach (var product in betterFilter.Filter(products, new AndSpecification <Product>(new ColorSpecification(Enums.Color.Green), new SizeSpecification(Enums.Size.Small)))) { Console.WriteLine($"{product.Name} is green and small"); } Console.ReadLine(); }
static void Main(string[] args) { Product apple = new Product("Apple", Color.Red, Size.Small); Product tree = new Product("Tree", Color.Green, Size.Large); Product house = new Product("House", Color.Blue, Size.Huge); Product[] products = { apple, tree, house }; Console.WriteLine("Green products(old)"); ProductFilter pf = new ProductFilter(); foreach (Product product in pf.FilterByColor(products, Color.Green)) { Console.WriteLine(product); } Console.WriteLine("Green products(new)"); ColorSpecification greenSpecification = new ColorSpecification(Color.Green); BetterFilter betterFilter = new BetterFilter(); IEnumerable <Product> greenProducts = betterFilter.Filter(products, greenSpecification); foreach (Product product in greenProducts) { Console.WriteLine(product); } Console.WriteLine("Green and Large products"); SizeSpecification largeSpecification = new SizeSpecification(Size.Large); AndSpecification <Product> andSpecification = new AndSpecification <Product>(greenSpecification, largeSpecification); IEnumerable <Product> andProducts = betterFilter.Filter(products, andSpecification); foreach (Product product in andProducts) { Console.WriteLine(product); } }