private static void DoTest01() { 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(); Console.WriteLine("Green products:"); ColorSpecification cs = new ColorSpecification(Color.Green); foreach (var p in bf.Filter(products, cs)) { Console.WriteLine($" - {p.Name} is green"); } Console.WriteLine("----- And Specification ----------"); SizeSpecification ss = new SizeSpecification(Size.Large); AndSpecification <Product> andSpecification = new AndSpecification <Product>(cs, ss); foreach (var p in bf.Filter(products, andSpecification)) { Console.WriteLine($" - {p.Name} is green and large"); } }
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(); WriteLine("Green products (old):"); foreach (var p in pf.FilterByColor(products, Color.Green)) { WriteLine($" - {p.Name} is green"); } // ^^ BEFORE // vv AFTER WriteLine("Green products (new):"); var bf = new BetterFilter(); foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green))) { WriteLine($" - {p.Name} is green"); } WriteLine("Large Blue items (new):"); 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) { Console.WriteLine("OpenClosed Priniciple"); var shorts = new Product("Shorts", Color.Red, Size.Small); var jeans = new Product("Jeans", Color.Blue, Size.Large); var pants = new Product("Pant", Color.Red, Size.Huge); Product[] products = { shorts, jeans, pants }; //Without OpenClosed(normal brute force) ProductFilter productFilter = new ProductFilter(); Console.WriteLine("Red Products"); foreach (var p in productFilter.FilterByColor(products, Color.Red)) { Console.WriteLine($"- {p.Name} is red"); } //With openclosed principle var betterFilter = new BetterFilter(); Console.WriteLine("Red Products using new filter"); foreach (var p in betterFilter.Filter(products, new ColorSpecification(Color.Red))) { Console.WriteLine($"- {p.Name} is red"); } foreach (var p in betterFilter.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Red), new SizeSpecification(Size.Huge)))) { Console.WriteLine($"{p.Name} is a Red colored product which is huge in size"); } }
static void Main(string[] args) { var apple = new Product("Apple", Color.Red, Size.Small); var tree = new Product("Tree", Color.Green, Size.Medium); var house = new Product("House", Color.Blue, Size.Large); Product[] products = { apple, tree, house }; 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 Products: "); foreach (var p in bf.Filter(products, new SizeSpecificiation(Size.Large))) { Console.WriteLine($" - {p.Name} is large"); } Console.WriteLine("Large Blue Products: "); foreach (var p in bf.Filter(products, new AndSpecification <Product>( new ColorSpecification(Color.Blue), new SizeSpecificiation(Size.Large)))) { Console.WriteLine($" - {p.Name} is large 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 car = new Product("Car", Color.Blue, Size.Large); var house = new Product("House", Color.Blue, Size.ExtraLarge); Product[] products = { apple, tree, car, house }; // old, violates Open-Closed Console.WriteLine("Green products (old):"); foreach (var p in ProductFilter.FilterByColor(products, Color.Green)) { Console.WriteLine($" - {p.Name} is green"); } // new var filter = new BetterFilter(); Console.WriteLine("Green products (new):"); foreach (var p in filter.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($" - {p.Name} is green"); } Console.WriteLine("Large blue items:"); foreach (var p in filter.Filter( products, new AndSpecification <Product>( new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large)))) { Console.WriteLine($" - {p.Name} is large 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 }; //b4 priciple var pf = new ProductFilter(); Console.WriteLine("Green Products (old):"); foreach (var p in pf.FilterByColor(products, Color.Green)) { Console.WriteLine($" - {p.Name}"); } //after applying principles var bf = new BetterFilter(); Console.WriteLine("Green Products (new):"); foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($" - {p.Name}"); } Console.WriteLine("Large Products (new):"); foreach (var p in bf.Filter(products, new SizeSpecification(Size.Large))) { Console.WriteLine($" - {p.Name}"); } Console.WriteLine("Large Blue Products (new):"); foreach (var p in bf.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large)))) { Console.WriteLine($" - {p.Name} is large and blue"); } Console.ReadLine(); }
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 Product (Old)"); foreach (var p in pf.FilterByColor(products, Color.Green)) { WriteLine($" - { p.Name} is Green"); } // // AFTER 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"); } 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) { Console.WriteLine("OpenClosedPrinciple Demo!"); var apple = new Product("Apple", Color.Green, Size.small); var mango = new Product("Mango", Color.Yellow, Size.medium); var House = new Product("House", Color.Red, Size.Large); Product[] products = { apple, mango, 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."); } BetterFilter 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 red item"); foreach (var p in bf.Filter (products, new AndSpecification <Product> (new ColorSpecification(Color.Red), new SizeSpecification(Size.Large) ))) { Console.WriteLine($"-{p.Name} is large and red."); } }
// The class should be opened for extension but should be closed for modification (inerfaces and inheritance) 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 }; // old one var pf = new ProductFilter(); Console.WriteLine("Green products old :"); foreach (var p in pf.FilterByColor(products, Color.Green)) { Console.WriteLine($" - {p.Name} is green"); } // new one //created ISpecification(condidion) and IFilter(checking condition) var bf = new BetterFilter(); Console.WriteLine("Green products new :"); foreach (var item in bf.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($" - {item.Name} is green"); } //using combined specification Console.WriteLine("Large blue items: "); foreach (var item in bf.Filter(products, new AndSpecification <Product>( new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large) ))) { Console.WriteLine($" - {item.Name} is large and blue"); } }
static void Main(string[] args) { Product apple = new Product("Apple", Color.Green, Size.Small); Product tree = new Product("Tree", Color.Green, Size.Small); Product house = new Product("House", Color.Blue, Size.Large); Product[] products = { apple, tree, house }; // old ProductFilter pf = new ProductFilter(); WriteLine("Green products (old):"); foreach (Product p in pf.FilterProductByColor(products, Color.Green)) { WriteLine($" - {p.Name} is green"); } // new BetterFilter bf = new BetterFilter(); WriteLine("\nGreen products (new):"); foreach (Product p in bf.Filter(products, new ColorSpecification(Color.Green))) { WriteLine($" - {p.Name} is green"); } // combinator WriteLine("\nLarge blue items"); foreach (Product p in bf.Filter( products, new AndSpecification <Product>( new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large) ))) { WriteLine($" - {p.Name} is large 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 productFilter = new ProductFilter(); Console.WriteLine("Green products (old):"); foreach (var product in productFilter.FilterByColor(products, 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(Color.Green))) { Console.WriteLine($"- {product.Name} is green"); } // ----------------------------------------------------------------------------- Console.WriteLine("Large blue items"); foreach (var product in betterFilter.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large)))) { Console.WriteLine($"- {product.Name} is big and blue."); } Console.ReadLine(); }