Beispiel #1
0
        static void Main(string[] args)
        {
            // Without Builder Pattern

            var hello = "Hello";
            var sb    = new StringBuilder();

            sb.AppendLine("<p>");
            sb.AppendLine(hello);
            sb.AppendLine("</p>");
            Console.WriteLine(sb);

            var words = new[] { "hello", "world" };

            sb.Clear();
            sb.AppendLine("<ul>");
            foreach (var word in words)
            {
                sb.AppendFormat("<li>{0}</li>", word);
            }

            sb.AppendLine("</ul>");

            Console.WriteLine(sb);

            // Builder Pattern Usage


            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "Hello");
            builder.AddChild("li", "World");
            Console.WriteLine(builder);
        }
        /* Builder creation design pattern */

        // Motivation :
        // 1- Some object are simple and can be created in a single constructor call
        // 2- Other object require a lot of ceremony to create
        // 3- HaHaving an object 10 constructor argument is not productive
        // 4- Instead, opt for piecewise construction
        // 5- Builder provides an API for constructing an object step by step
        static void Main(string[] args)
        {
            var hello = "hello";
            var sb    = new StringBuilder();

            sb.Append("<p>");
            sb.Append(hello);
            sb.Append("</p>");
            Console.WriteLine(sb);  // simple example of builder pattern

            var words = new [] { "Hello", "World" };

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

            sb.Append("</ul>");

            Console.WriteLine(sb);

            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "hello");
            builder.AddChild("li", "world");

            Console.WriteLine(builder.ToString());

            Console.ReadKey();
        }
Beispiel #3
0
        public void Demo()
        {
            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "hello");
            builder.AddChild("li", "world");
            Console.WriteLine(builder.ToString());
        }
        static void Main(string[] args)
        {
            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "hello");
            builder.AddChild("li", "world");
            Console.WriteLine(builder.ToString());
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "Hello");
            builder.AddChild("li", "world");
            WriteLine(builder.ToString());
            ReadLine();
        }
Beispiel #6
0
        static void WithBuilder()
        {
            var builder = new HtmlBuilder("ul");

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

            // Fluent
            builder.Clear();
            builder.AddChild("li", "hello").AddChild("li", "world");
            Console.WriteLine(builder);
        }
        static void Main(string[] args)
        {
            var hello = "hello";
            var sb    = new StringBuilder();

            sb.Append("<p>");
            sb.Append(hello);
            sb.Append("</p>");
            Console.WriteLine(sb);

            var words = new[] { "hello", "world" };

            sb.Clear();
            sb.Append("<ul>");
            foreach (var word in words)
            {
                sb.AppendFormat($" {word}");
            }
            sb.Append("</ul>");

            Console.WriteLine(sb);


            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "hello").AddChild("li", "world");
            Console.WriteLine(builder.ToString());
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "hello").AddChild("li", "blorb");
            Console.WriteLine(builder);
        }
        static void Main(string[] args)
        {
            var hello = "hello";
            var sb    = new StringBuilder();

            sb.Append("<p>");
            sb.Append(hello);
            sb.Append("/<p>");
            Console.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>");
            Console.WriteLine(sb);

            var builder = new HtmlBuilder("ul");

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

            var me = Person.New.Called("Manuel").WorksAs("MyJob").Build();

            Console.WriteLine(me);

            var           apb    = new AnotherPersonBuilder();
            AnotherPerson person = apb.Works.At("Fabrikam").AsA("Eng").Earning(100000).Lives.At("123 London").In("London").WithPostCode("123");

            Console.WriteLine(person);
        }
Beispiel #10
0
        static void Main(string[] args)
        {
            var hello = "hello";
            var sb    = new StringBuilder();

            sb.Append(hello);
            Console.WriteLine(sb);

            var words = new[] { "hello", "world" };

            sb.Clear();
            foreach (var word in words)
            {
                sb.Append(word);
            }

            Console.WriteLine(sb);

            var builder = new HtmlBuilder("ul");

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

            Console.ReadKey();
        }
Beispiel #11
0
        static void Main(string[] args)
        {
            //var hello = "hello";
            //var sb = new StringBuilder();
            //sb.Append("<p>");
            //sb.Append(hello);
            //sb.Append("</p>");
            //Console.WriteLine(sb);

            //List<string> words = new List<string> {"hello", "world"};

            //sb.Clear();
            //sb.Append("<ul>");
            //foreach (var w in words)
            //{
            //    sb.Append($"<li>{w}</li>");
            //}
            //sb.Append("</ul>");
            //Console.WriteLine(sb);


            var builder = new HtmlBuilder("ul");

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


            Console.ReadKey();
        }
        public static void Main()
        {
            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "Hello").AddChild("li", "world");

            Console.WriteLine(builder);
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            var builder = new HtmlBuilder("ul");

            builder.AddChild("li", "hello").AddChild("li", "world");

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

            builder.AddChild("li", "hello").AddChild("li", "world")
            .AddChild("li", @"This world
is full of shit.
Though, we have what
we have.");
            Console.WriteLine(builder);
        }
        // change to main to run.
        public static void none(string[] args)
        {
            // OLD
            //var hello = "hello";
            //var sb = new StringBuilder();
            //sb.Append("<p>");
            //sb.Append(hello);
            //sb.Append("<p>");
            //Console.WriteLine(sb);

            //// too much complexity to do deeper formatting
            //// an HTMLBuilder would be necessary here.
            //// So that you can build HTML objects with a good
            //// understandable clear api (succinct)

            //string[] words = new[] { "hello", "world" };
            //sb.Clear();
            //sb.Append("<ul>");

            //foreach (string word in words)
            //{
            //    sb.AppendFormat("<li>{0}</li>", word);
            //}

            //sb.Append("</ul>");
            //Console.WriteLine(sb);

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

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

            // NEW (fluent chaining / chaining nodes)
            var fluentBuilder = new HtmlBuilder("ul");

            fluentBuilder.AddChild("li", "hello")
            .AddChild("li", "world");
            Console.WriteLine(fluentBuilder);
        }
Beispiel #16
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 #17
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 #18
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 #19
0
        static void Main(string[] args)
        {
            var sb = new StringBuilder();

            sb.Append("<p>");
            sb.Append("hola");
            sb.Append("</p>");
            Console.WriteLine(value: sb);

            var htmlBuilder = new HtmlBuilder("ul");

            htmlBuilder.AddChild("li", "name");
            Console.WriteLine(htmlBuilder);
        }
Beispiel #20
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();
        }
Beispiel #21
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 #22
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 #23
0
        //Creating html tags without builder pattern
        static void Main(string[] args)
        {
            //putting hello in html paragraph tag
            var hello = "Hello";
            var sb    = new StringBuilder();

            sb.Append("<p>");
            sb.Append(hello);
            sb.Append("</p>");
            Console.WriteLine(sb);

            //creating a list
            var words = new[] { "hello", "world" };

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

            //Creating a list Using Builder
            var builder = new HtmlBuilder("ul");

            //builder.AddChild("li", "Hello");
            //builder.AddChild("li", "World");
            //foreach (var word in words)
            //{
            //    builder.AddChild("li", word);
            //}

            //This is possible because of fluent interface
            builder.AddChild("li", "Hello").AddChild("li", "World");
            Console.WriteLine(builder);
        }