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 }