Beispiel #1
0
        static void Main(string[] args)
        {
            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "Hello");
            builder.AddChild("li", "world");
            WriteLine(builder.ToString());
            ReadLine();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var words = new string[] { "hello", "world" };

            var builder = new HtmlBuilder("ul");

            foreach (var word in words)
            {
                builder.AddChild("li", word);
            }
            Console.WriteLine(builder.ToString());
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            // In .NET Framework the StringBuilder implements the Builder pattern already
            var hello = "hello";
            var sb    = new StringBuilder();

            sb.Append("<p>");
            sb.Append(hello);
            sb.Append("</p>");
            Console.WriteLine(sb);
            sb.Clear();
            // This way to build HTML elements is too complex
            var words = new[] { "hello", "world" };

            sb.Append("<ul>");
            foreach (var word in words)
            {
                sb.AppendFormat("<li>{0}</p>", word);
            }
            sb.Append("</ul>");
            Console.WriteLine(sb);

            // --------------------------------------------------------------------------------
            // Now the code is structured in a more Object Oriented way
            // having an HtmlBuilder which his own purpose is to build html elements
            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "hello").AddChild("li", "world");;
            Console.WriteLine(builder.ToString());

            Console.ReadLine();

            // -------------------------------------------------------------------------------

            var pb = new PersonBuilder();
            // The reason why we can jump from building the address to the employment info
            // is because both, the address and the employment builder inherit from PersonBuilder
            // so both of them expose every other builder
            // A side effect is that you can do -> person.Lives.Lives.Lives...
            Person person = pb.Lives.At("123 London Road").WithPostcode("SW12AC")
                            .Works.At("Acme").AsA("Coyote").Earning(23000);

            // We've used two sub-builders to present an interface that is nice and convenient, but the object
            // we built isn't a Person, it appears like PersonBuilder, to avoid that happen we use
            // public static implicit operator Person(PersonBuilder pb) and return pb.person
            Console.WriteLine(person.ToString());

            Console.ReadLine();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            //call non fluent builer
            HtmlBuilder builder = new HtmlBuilder("ul");

            builder.AddChild("li", "one");
            builder.AddChild("li", "two");
            builder.AddChild("li", "three");
            Console.WriteLine(builder.ToString());
            //call fluent buiilder pattern
            Employee emp = Employee.New.SetName("AMOSE").WorkingAs("Architect").Build();

            //faceted builder
            PersonBuilder v      = new PersonBuilder();
            Person        person = v
                                   .lives.LivesAt("tuty")
                                   .AreaCode("628008")
                                   .works.AsA("software enginier").
                                   At("lynk").
                                   Earning(0000);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Builder Pattern!");

            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "Hello");
            builder.AddChild("li", "World!");
            builder.ToString();
            Console.WriteLine(builder);


            //fluent builder
            var fluentBuilder = new HtmlBuilder("ul");

            fluentBuilder
            .AddChild("li", "Hello")
            .AddChild("li", "World!")
            .AddChild("li", "Fluent Builder.")
            .ToString();
            Console.WriteLine(fluentBuilder);
        }
Beispiel #6
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello Builder!");
            // 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);
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            #region Creation Builder
            //Simple HTML Paragraph using StringBuilder
            var hello = "hello";
            var sb    = new StringBuilder();
            sb.Append("<p>");
            sb.Append(hello);
            sb.Append("</p>");
            Console.WriteLine(sb);
            Console.WriteLine();
            //Html List With 2 words
            var words = new[] { "Hello", "World" };
            sb.Clear();
            sb.Append("<ul>");
            foreach (var word in words)
            {
                sb.Append($"<li>{word}</li>");
            }
            sb.Append("</ul>");
            Console.WriteLine(sb);
            Console.WriteLine();
            //ordinary non fluent builder
            var builder = new HtmlBuilder("ul");
            builder.AddChild("li", "hello");
            builder.AddChild("li", "world");
            Console.WriteLine(builder.ToString());

            //fluent builder
            sb.Clear();
            builder.Clear();
            builder.AddChildFluent("li", "hello").AddChildFluent("li", "world");
            Console.WriteLine(builder);
            #endregion

            Console.ReadKey();
        }