private static void BuilderPattern() { HtmlBuilder hb = new HtmlBuilder("ul"); hb.AddChild("li", "Hello World from ul li"); Console.WriteLine(hb); hb = new HtmlBuilder("p"); hb.AddChild("a", "Hello world from paragraph"); hb.AddChild("a", "Hello world from paragraph2"); hb.AddChild("li", "li tag").AddChild("li", "chaining in builder"); Console.WriteLine(hb); //var builder = new PersonJobBuilder(); //builder.Called("Saba"); //.WorksAs var emp = Employee .New .WorksAs("Developer") .Called("Saba") .Build(); Console.WriteLine(emp); var sb = new StudentBuilder(); Student student = sb .Studies.At("TSU") .On("Exact and natural sciences") .Is.Called("Saba"); Console.WriteLine(); }
static void RunBuilder() { HtmlBuilder builder = new HtmlBuilder("ul"); builder.AddChild("li", "hello").AddChild("li", "world"); Console.WriteLine(builder.ToString()); }
static void Main(string[] args) { // Factory Pattern var fridge = Samsung.Factory.GetNewFridge(); var washingMachine = Samsung.Factory.GetNewWashingMachine(); Console.WriteLine(fridge.ToString()); Console.WriteLine(washingMachine.ToString()); // Fluent Builder Pattern var htmlBuilder = new HtmlBuilder("html"); htmlBuilder.AddChild("head", "").AddChild("body", "<p>Hello World!</p>").AddChild("script", ""); Console.WriteLine(htmlBuilder.ToString()); // Prototype Patten var hondaAmaze = new Car(5, new Engine(1600)); var hondaCity = new Car(hondaAmaze); hondaCity.Engine.Capacity = 2000; Console.WriteLine(hondaAmaze); Console.WriteLine(hondaCity); // Bridge Pattern var circle = new Circle(new Pencil(), 5); circle.Draw(); circle = new Circle(new Pen(), 5); circle.Draw(); Console.ReadLine(); }
static void Main(string[] args) { var hello = "Hello"; var sb = new StringBuilder(); sb.Append("<p>"); sb.Append(hello); sb.Append("</p>"); WriteLine(sb); var words = new[] { "hello", "world" }; sb.Clear(); sb.Append("<ul>"); foreach (var word in words) { sb.AppendFormat("<li>{0}</li>", word); } sb.Append("</ul>"); WriteLine(sb); var builder = new HtmlBuilder("ul"); builder.AddChild("li", "hello").AddChild("li", "world"); WriteLine(builder.ToString()); }
public static void Test() { // Non-fluent builder var builder = new HtmlBuilder("ul"); builder.AddChild("li", "hello"); builder.AddChild("li", "world"); WriteLine(builder.ToString()); // Fluent Builder var sb = new StringBuilder(); sb.Clear(); builder.Clear(); builder.AddChildFluent("li", "hello").AddChildFluent("li", "world"); WriteLine(builder); }
static void Main(string[] args) { Console.WriteLine(); Console.WriteLine("Classic Builder:"); Console.WriteLine(); var builder = new HtmlBuilder("ul"); builder.AddChild("li", "Hello"); builder.AddChild("li", "World"); Console.WriteLine(builder.ToString()); Console.WriteLine(); Console.WriteLine("Fluent Builder with Recursive Generics:"); Console.WriteLine(); var person = RecursiveGenericsForFluentInheritance.Person.New .Called("Peter Johnson") .WorksAs("Journalist") .Build(); Console.WriteLine(person); Console.WriteLine(); Console.WriteLine("Faceted Builder:"); Console.WriteLine(); var pb = new FacetedBuilder.PersonBuilder(); FacetedBuilder.Person facetedPerson = pb.Works .At("Toyota") .AsA("Engineer") .Earning(123000) .Lives .At("123 london Road") .In("London") .WithPostcode("ERD DFR"); Console.WriteLine(facetedPerson); Console.ReadKey(); }
static void Mainn(string[] args) { // if you want to build a simple HTML paragraph using StringBuilder var hello = "hello"; var sb = new StringBuilder(); sb.Append("<p>"); sb.Append(hello); sb.Append("</p>"); WriteLine(sb); // now I want an HTML list with 2 words in it var words = new[] { "hello", "world" }; sb.Clear(); sb.Append("<ul>"); foreach (var word in words) { sb.AppendFormat("<li>{0}</li>", word); } sb.Append("</ul>"); WriteLine(sb); // ordinary non-fluent builder var builder = new HtmlBuilder("ul"); builder.AddChild("li", "hello"); builder.AddChild("li", "world"); WriteLine(builder.ToString()); // fluent builder sb.Clear(); builder.Clear(); // disengage builder from the object it's building, then... builder.AddChildFluent("li", "hello").AddChildFluent("li", "world"); WriteLine(builder); }
static void Main(string[] args) { WriteLine("\nDesign Patterns ...\n"); // SOLID #region SOLID #region Single Responsibility Principle #endregion #region Open-Closed Principle #endregion #region Liskov Substitution Principle #endregion #region Interface Segregation Principle #endregion #region Dependency Inversion Principle #endregion #endregion // Design Patterns #region Design Patterns #region Creational WriteLine("Creational Design Patterns ...\n"); #region Builder WriteLine("Builder ..."); // without builder WriteLine("\nWithout builder ..."); var hello = "hello"; var sb = new StringBuilder(); sb.Append("<p>"); sb.Append(hello); sb.Append("</p>"); WriteLine(sb); var words = new[] { "hello", "world" }; sb.Clear(); sb.Append("<ul>"); foreach (var word in words) { sb.AppendFormat("<li>{0}</li>", word); } sb.Append("</ul>"); WriteLine(sb); // with builder WriteLine("\nWith builder ..."); var builder = new HtmlBuilder("ul"); builder.AddChild("li", "hello"); builder.AddChild("li", "world"); WriteLine(builder.ToString()); WriteLine(""); #endregion #endregion #region Structural #endregion #region Behavioral #endregion #endregion }