public static void Run() { var parent = new Person { Name = "Adam" }; var child1 = new Person { Name = "Abel" }; var child2 = new Person { Name = "Cain" }; var relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); // consumer has no direct access to the data in relationships // but is aware that this data is accessible via interface var children = relationships.FindAllChildrenOf("Adam"); foreach (var child in children) { Console.WriteLine($"Adam has a child named {child.Name}"); } }
static void Main(string[] args) { /* * var j = new Journal(); * j.AddEntry("I cried today."); * j.AddEntry("I ate a bug."); * WriteLine(j); * * var p = new Persistence(); * var filename = @"c:\temp\journal.txt"; * p.SaveToFile(j, filename, true); * Process.Start(@"cmd.exe ", @"/c " + filename); */ #region Builder //DesignPattern.HtmlBuilder b = new DesignPattern.HtmlBuilder("html"); //var e = b.AddChild("ul", ""); //b.AddChild("li", "hola", e); //b.AddChild("li", "mundo", e); //WriteLine(b.ToString()); #endregion Builder #region Dependency Inversion Principle var parent = new Person { Name = "Marco" }; var child1 = new Person { Name = "Juan" }; var child2 = new Person { Name = "Antonella" }; var child3 = new Person { Name = "Bathroom" }; var relationship = new Relationships(); relationship.AddParentAndChild(parent, child1); relationship.AddParentAndChild(parent, child2); relationship.AddParentAndChild(parent, child3); var research = new Research(relationship); #endregion Dependency Inversion Principle ReadKey(); }
public static void Main(string[] args) { var parent = new Person { Name = "John" }; var child1 = new Person { Name = "Chris" }; var child2 = new Person { Name = "Mary" }; var relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); }
public static void Run() { var parent = new Person { Name = "John" }; var child1 = new Person { Name = "Chris" }; var child2 = new Person { Name = "Mary" }; var relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); new Research(relationships); }
static void RunDependecyInversionPrinciple() { SOLIDDesignPrinciple.Person parent = new SOLIDDesignPrinciple.Person { Name = "John" }; SOLIDDesignPrinciple.Person child1 = new SOLIDDesignPrinciple.Person { Name = "Chris" }; SOLIDDesignPrinciple.Person child2 = new SOLIDDesignPrinciple.Person { Name = "Mary" }; Relationships relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); new Research(relationships); }
private static void DependencyInversion() { var parent = new Person { Name = "John" }; var child1 = new Person { Name = "Chris" }; var child2 = new Person { Name = "Mary" }; var relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); var research = new Research(relationships); }
static void Main(string[] args) { var parent = new Person { Name = "John" }; var child1 = new Person { Name = "Chris" }; var child2 = new Person { Name = "Matt" }; // low-level module var relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); new Research(relationships); }
private void DependencyInversion() { //we're just doing kind of setup by adding relationships, but after that we want to perform the research var parent = new Person { Name = "John" }; var child1 = new Person { Name = "Chris" }; var child2 = new Person { Name = "Mary" }; var relationships = new Relationships(); //so we want to take this low level module and we want to access it in a high level module(i.e. Research) relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); new Research(relationships); new Research(browser: relationships); }
static void SOLIDInterfaceSegregation() { var parent = new Person { Name = "John" }; var child1 = new Person { Name = "Chris" }; var child2 = new Person { Name = "Matt" }; // low-level module var relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); new Research(relationships); Console.ReadLine(); }
static void Main(string[] args) { var parent = new Person() { Name = "Cristian" }; var child1 = new Person() { Name = "Alex" }; var child2 = new Person() { Name = "Oana" }; var relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); var rc = new Research(relationships); }
static void Main(string[] args) { // 1 - SINGLE RESPONSIBILITY PRINCIPLE START //var j = new Journal(); //j.AddEntry("I cried today."); //j.AddEntry("I ate a bug."); //WriteLine(j); //var p = new Persistence(); //var filename = @"c:\temp\journal.txt"; //p.SaveToFile(j, filename); //Process.Start(filename); // 1 - SINGLE RESPONSIBILITY PRINCIPLE END // 2 - OPEN CLOSED PRINCIPLE START //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 //var bf = new BetterProductFilter(); //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"); //} // 2 - OPEN CLOSED PRINCIPLE END // 3 - LISKOV SUBSTITUTION PRINCIPLE START //Rectangle rc = new Rectangle(2, 3); //WriteLine($"{rc} has area {Area(rc)}"); //// should be able to substitute a base type for a subtype ///*Square*/ //Rectangle sq = new Square(); //sq.Width = 4; //WriteLine($"{sq} has area {Area(sq)}"); // 3 - LISKOV SUBSTITUTION PRINCIPLE END // 4 - INTERFACE SEGREGATION PRINCIPLE START // Look inside the folder for example // 4 - INTERFACE SEGREGATION PRINCIPLE END // 5 - DEPENDENCY INVERSION PRINCIPLE START // hl modules should not depend on low-level; both should depend on abstractions // abstractions should not depend on details; details should depend on abstractions var parent = new Person { Name = "John" }; var child1 = new Person { Name = "Chris" }; var child2 = new Person { Name = "Matt" }; // low-level module var relationships = new Relationships(); relationships.AddParentAndChild(parent, child1); relationships.AddParentAndChild(parent, child2); new Research(relationships); // 5 - DEPENDENCY INVERSION PRINCIPLE END ReadLine(); }
public static void Main(string[] args) { #region Single responsibility var j = new Journal(); j.AddEntry("hi"); j.AddEntry("hello world"); Console.WriteLine(j); //Console.ReadKey(); var p = new Persistance(); string fileName = @"C:\LOG\test.txt"; p.SaveFile(j, fileName, true); Process.Start(fileName); #endregion #region Open-closed principle 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 nf = new NewFilter(); Console.WriteLine("Green products"); var result1 = nf.Filter(products, new ColorSpecification(Color.Green)); foreach (var i in result1) { Console.WriteLine($" - {i.Name} is green"); } Console.WriteLine("Large products"); var result2 = nf.Filter(products, new SizeSpecification(Size.Large)); foreach (var i in result2) { Console.WriteLine($" - {i.Name} is large"); } Console.WriteLine("Blue and Large products"); var result3 = nf.Filter(products, new AndSpecification <Product> (new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large) ) ); foreach (var i in result3) { Console.WriteLine($" - {i.Name} is large and blue"); } Console.ReadKey(); #endregion #region liskov principle Rectangle rec = new Rectangle(4, 3); var d = new Demo(); Console.WriteLine($"the area of the rectangle {rec} is {d.Area(rec)}"); Rectangle sq = new Square(); sq.Width = 5; Console.WriteLine($"the area of the square {sq} is {d.Area(sq)}"); Console.ReadKey(); #endregion #region DI principle var parent = new Person("John"); var child1 = new Person("Matt"); var child2 = new Person("Test"); Relationships rel = new Relationships(); rel.AddParentAndChild(parent, child1); rel.AddParentAndChild(parent, child2); Research rs = new Research(rel); Console.ReadKey(); #endregion }