private 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 }; var productFilter = new ProductFilter(); System.Console.WriteLine("Green products (old):"); foreach (var product in productFilter.FilterByColour(products, Colour.Green)) { System.Console.WriteLine($" - {product.Name} is {product.Colour}"); } var betterFilter = new BetterFilter(); System.Console.WriteLine("GreenProducts (new)"); foreach (var item in betterFilter.Filter(products, new CololourSpecification(Colour.Green))) { System.Console.WriteLine($" - {item.Name} is {item.Colour}"); } System.Console.WriteLine("For large blue items"); foreach (var item in betterFilter.Filter(products, new AndSpecification <Product>(new CololourSpecification(Colour.Blue), new SizeSpecification(Size.Large)))) { System.Console.WriteLine($" - {item.Name} is {item.Colour}"); } System.Console.ReadLine(); }
public static void OPC() { 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); var products = new Product[] { apple, tree, house }; var pf = new ProductFilter(); WriteLine("Green products (old):"); foreach (var p in pf.FilterByColor(products, Color.Green)) { WriteLine($" - {p._name} is {p._color}"); } var bf = new BetterFilter(); WriteLine("Green products (new): "); foreach (var item in bf.Filter(products, new ColorSpecification(Color.Green))) { WriteLine($" - {item._name} is {item._color}"); } WriteLine("Large blue items"); foreach (var item in bf.Filter(products, new AndSpecification <Product>( new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large) ))) { WriteLine($" - {item._name} is {item._color}"); } }
private void OpenClosed() { 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 item in productFilter.FilterByColor(products, Color.Green)) { Console.WriteLine($"{item.Name} is green"); } var betterFilter = new BetterFilter(); Console.WriteLine("Green products (new):"); foreach (var item in betterFilter.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($"{item.Name} is green"); } Console.WriteLine("Large blue items"); foreach (var item in betterFilter.Filter( products, new AndSpecification <Product>( new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large)))) { Console.WriteLine($"{item.Name} is large and blue"); } }
public static void Execute() { 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 }; //implementing a better way var bf = new BetterFilter(); Console.Write("Green products:"); foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($" - {p.Name} is green"); } Console.Write("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 blue and large."); } }
public static void TestOpenClosePrinciple() { var apple = new Product("Apple", Color.Red, Size.Medium); var tree = new Product("Tree", Color.Yellow, Size.Big); var house = new Product("House", Color.Blue, Size.Mediocore); Product[] products = { apple, tree, house }; Console.WriteLine("Red Products(old): "); foreach (var p in ProductFilter.FilterByColor(products, Color.Red)) { Console.WriteLine($"{p.Name} is {p.Color}"); } BetterFilter bf = new BetterFilter(); Console.WriteLine("Red Products(new): "); foreach (var p in bf.Filter(products, new ColorSpecification(Color.Red))) { Console.WriteLine($"{p.Name} is {p.Color}"); } Console.WriteLine("Red Products(new): "); foreach (var p in bf.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Red), new SizeSpecification(Size.Mediocore)))) { Console.WriteLine($"{p.Name} is {p.Color} and {p.Size}"); } }
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."); } 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 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 bf = new BetterFilter(); WriteLine("Gree products (new):"); foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green))) { WriteLine($" - { p.Name} is green"); } /* * var j = new Journal(); * j.AddEntry("I happy today"); * j.AddEntry("I ate a hot dog"); * Console.WriteLine(j); * * var p = new Persistence(); * var filename = @"C:\Users\luisc\Documents\GitHub\journal.txt"; * p.SaveToFile(j, filename, true); * * Process.Start(filename); */ }
// OCP = open for extension but closed for modification public void MainCall() { 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 AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))) ) { Console.WriteLine($" - {p.Name} is big and blue"); } }
public void Filter_GetMediumProducts_ReturnEmptyList() { IEnumerable <Product> products = new List <Product>() { new Product("Polo Shirt", ProductColor.Black, ProductSize.L), new Product("T-Shirt", ProductColor.Blue, ProductSize.XS), new Product("T-Shirt", ProductColor.Red, ProductSize.S), new Product("Jeans", ProductColor.Blue, ProductSize.XL), new Product("Jeans", ProductColor.Black, ProductSize.XL), }; var betterFilter = new BetterFilter(); var filteredProducts = betterFilter.Filter(products, new SizeSpecification(ProductSize.M)).ToList(); Assert.That(filteredProducts.Count, Is.EqualTo(0)); return; }
public static void Run() { var products = new List <Product> { new Product("Apple", Color.Green, Size.Small), new Product("Carrot", Color.Red, Size.Small), new Product("Blueberry", Color.Blue, Size.Small) }; var bf = new BetterFilter(); foreach (var p in bf.Filter(products, new ColorSpecification(Color.Red))) { Console.WriteLine($" - {p.Name} is red"); } }
public void Filter_ByColorsWithOr_ReturnsCorrectCount() { var products = new List <Product>() { new Product("P1", ProductColor.Black, ProductSize.L), new Product("P2", ProductColor.Blue, ProductSize.M), new Product("P3", ProductColor.Green, ProductSize.M), new Product("P4", ProductColor.Blue, ProductSize.S), new Product("P5", ProductColor.Black, ProductSize.XL), }; var betterFilter = new BetterFilter(); var filteredProducts = betterFilter.Filter(products, new OrSpecification <Product>( new ColorSpecification(ProductColor.Black), new ColorSpecification(ProductColor.Blue))); Assert.That(filteredProducts.Count, Is.EqualTo(4)); }
public void SOLID_OCP_Test4() { var betterFilter = new BetterFilter(); var filteredProductNamesByColorAndSize = new List <string>(); var largeGreenSpec = Color.Green.And(Size.Small); foreach (var item in betterFilter.Filter(products, largeGreenSpec)) { filteredProductNamesByColorAndSize.Add(item.Name); } IEnumerable <string> expectedProductNames = products.Where(q => q.Color == Color.Green && q.Size == Size.Small) .Select(q => q.Name); Assert.IsTrue(HelperTest.AreEquals(expectedProductNames, filteredProductNamesByColorAndSize.ToArray(), order: true)); }
public void SOLID_OCP_Test1() { IEnumerable <string> expectedProductNames = new List <string>(); var betterFilter = new BetterFilter(); List <string> filteredProductNamesByColor = new List <string>(3); foreach (var item in betterFilter.Filter(products, new ColorSpecification(Color.Green))) { filteredProductNamesByColor.Add(item.Name); } expectedProductNames = products.Where(q => q.Color == Color.Green) .Select(q => q.Name); Assert.IsTrue(HelperTest.AreEquals(expectedProductNames, filteredProductNamesByColor.ToArray(), order: true)); }
public void Filter_GetLargeBlackProducts_ReturnListWithSingleElement() { IEnumerable <Product> products = new List <Product>() { new Product("Polo Shirt", ProductColor.Black, ProductSize.L), new Product("T-Shirt", ProductColor.Blue, ProductSize.XS), new Product("T-Shirt", ProductColor.Red, ProductSize.S), new Product("Jeans", ProductColor.Blue, ProductSize.XL), new Product("Jeans", ProductColor.Black, ProductSize.XL), }; var betterFilter = new BetterFilter(); var andSpecification = new AndSpecification <Product>( new ColorSpecification(ProductColor.Black), new SizeSpecification(ProductSize.L)); var filteredProducts = betterFilter.Filter(products, andSpecification).ToList(); Assert.That(filteredProducts.Count, Is.EqualTo(1)); return; }
static void Main(string[] args) { #region OCP var apple = new Product("Jabłko", Color.Green, Size.Small); var tree = new Product("Drzewo", Color.Green, Size.Large); var house = new Product("Dom", Color.Blue, Size.Large); Product[] products = { apple, tree, house }; var bf = new BetterFilter(); var largeGreenSpec = new ColorSpecification(Color.Green) & new SizeSpecification(Size.Large); foreach (var p in bf.Filter(products, largeGreenSpec)) { Console.WriteLine($" - {p.Name} jest zielony"); } #endregion }
static void Main(string[] args) { // 01 - SRP var j = new Journal(); j.AddEntry("I cried today"); j.AddEntry("I ate a bug"); Console.WriteLine(j); var p = new Persistence(); var filename = @"Journal.txt"; p.SaveToFIle(j, filename, true); Process.Start(filename); // 02 - OCP 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 item in pf.FilterByColor(products, Color.Green)) { Console.WriteLine($" - {item.Name} is green"); } var bf = new BetterFilter(); Console.WriteLine("Green Product (New):"); foreach (var item in bf.Filter(products, new ColorSpecification(Color.Green))) { Console.WriteLine($" - {item.Name} is green"); } Console.WriteLine("Large Product (New):"); foreach (var item in bf.Filter(products, new SizeSpecification(Size.Large))) { Console.WriteLine($" - {item.Name} is large"); } Console.WriteLine("Large Blue Product (New):"); foreach (var item in bf.Filter(products, new AndSpecification <Product>(new SizeSpecification(Size.Large), new ColorSpecification(Color.Blue)))) { Console.WriteLine($" - {item.Name} is big and blue"); } // 03 - LSP var rc = new Rectangle(2, 3); Console.WriteLine($"{rc} has area {Area(rc)}"); Rectangle sq = new Square(); sq.Width = 4; Console.WriteLine($"{sq} has area {Area(sq)}"); // 05 - DIP var parent = new Person() { Name = "John" }; var child1 = new Person() { Name = "Chris" }; var child2 = new Person() { Name = "Mary" }; var relationships = new DIP.Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); _ = new DIP.Research(relationships); }
static void Main(string[] args) { //demo1 var devCalculations = new List <BaseSalaryCalculator> { new SeniorDevSalaryCalculator(new DeveloperReport { Id = 1, Name = "Dev1", Level = "Senior developer", HourlyRate = 30.5, WorkingHours = 160 }), new JuniorDevSalaryCalculator(new DeveloperReport { Id = 2, Name = "Dev2", Level = "Junior developer", HourlyRate = 20, WorkingHours = 150 }), new SeniorDevSalaryCalculator(new DeveloperReport { Id = 3, Name = "Dev3", Level = "Senior developer", HourlyRate = 30.5, WorkingHours = 180 }) }; //SalaryCalculator class is now closed for modification and opened for an extension, which is exactly what OCP states. var calculator = new SalaryCalculator(devCalculations); Console.WriteLine($"Sum of all the developer salaries is {calculator.CalculateTotalSalaries()} dollars"); //demo2 //old var apple = new Product("Apple", Color.Green, Size.Small); var tree = new Product("Tree", Color.Green, Size.Large); var house = new Product("House", Color.Red, 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"); } //new 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"); } //filter with two specification Console.WriteLine("Large red items"); foreach (var product in betterFilter.Filter( products, new AndSpecification <Product>( new ColorSpecification(Color.Red), new SizeSpecification(Size.Large)))) { Console.WriteLine($" - {product.Name} big and red"); } Console.WriteLine(); Console.WriteLine("DEMO3"); //demo3 new DEMO3.Demo3().Execute(); Console.ReadLine(); }
private static void SolidPrinciples() { var journal = new Journal(); journal.AddEntry("I Started today Udemy."); journal.AddEntry("I started with Solid."); Console.WriteLine(journal); var persistence = new Persistence(); var fileName = @"C:\dev\designPatterns\journal.txt"; persistence.SaveToFile(journal, fileName, true); var apple = new Product("apple", Enums.Color.Green, Enums.Size.Small); var orange = new Product("orange", Enums.Color.Red, Enums.Size.Small); var tree = new Product("tree", Enums.Color.Blue, Enums.Size.Large); var house = new Product("house", Enums.Color.Blue, Enums.Size.Large); Product[] products = { apple, tree, house, orange }; var productFilter = new ProductFilter(); Console.WriteLine("Blue products (old): "); foreach (var p in productFilter.FilterByColor(products, Enums.Color.Blue)) { Console.WriteLine($" - {p.Name} is Blue"); } var betterFilter = new BetterFilter(); var colorSpec = new ColorSpecification(Enums.Color.Blue); Console.WriteLine("Blue products (new): "); foreach (var p in betterFilter.Filter(products, colorSpec)) { Console.WriteLine($" - {p.Name} is Blue"); } var sizeSpec = new SizeSpecification(Enums.Size.Large); var doubleSpec = new AndSpecification <Product>(colorSpec, sizeSpec); Console.WriteLine("Blue and Large products (new): "); foreach (var p in betterFilter.Filter(products, doubleSpec)) { Console.WriteLine($" - {p.Name} is Blue and large"); } var rectangle = new Rectangle(2, 3); Console.WriteLine($"{rectangle} has area {Area(rectangle)}"); Rectangle square = new Square(); square.Width = 3; Console.WriteLine($"{square} has area {Area(square)}"); var parent = new PersonDependencyInversion { Name = "Otman" }; var child1 = new PersonDependencyInversion { Name = "Loreto" }; var child2 = new PersonDependencyInversion { Name = "Emilio" }; rS.AddParentChild(parent, child1); }