Lecture thumbnail 0:00 / 0:00 All right.

So let’s talk about the builder pattern.

Why do we need it?

Well, the reason why the builder pattern exists is because sometimes you just want a bit of convenience

when building up objects, especially if those objects are complicated.

So let’s take a look at an example where we don’t have the use of the builder pattern.

And we’re going to begin by considering a scenario where you want to output a chunk of HTML.

Let’s suppose you just want a single paragraph with a piece of text in it.

So let’s suppose I want to have some string being output.

So let’s say I have a string called hello, which is just going to contain hello.

And I want it turned into an HTML paragraph.

So what do I do?

Well, it turns out that in the net framework we already have a component which implements a kind of

builder, and that is of course, the string builder.

The name kind of says it all and that is used for building up strings.

So we’re going to use it to build up an HTML string.

So let’s say var SB equals new string builder.

So I want to build up a string which starts with the paragraph tag.

So I’m going to say SB dot append.

I’m going to open up the paragraph tag and then I’m going to add the actual string that I want to have

in here.

In our case, that’s Hello.

And then I’m going to close the tag.

So having written something like this, I can write line SB because Stringbuilder has a two string implementation

which actually outputs the string you’ve built up.

And if I execute this well, the result is predictable enough.

We’re going to get the paragraph tag and then we’re going to get the hello string in between, and that’s

exactly what we get.

So that’s already a small example of using a very low level kind of builder, which simply builds up

strings.

Now let’s suppose that I want something a bit more complicated than a single paragraph.

Let’s suppose that I want an HTML list with two words containing the actual item.

So we’re going to have the words.

So we’re going to have the words as an array with Hello and world and we’re going to actually clear

the string builder.

I’m going to reuse the one I have already, and then I’m going to append the tag for the unordered list.

So that’s Ul.

And then at some point down the line, I’m going to close this, so I’ll have slash Ul and in between

I’ll put a for each where we go through all the elements in words.

So we’re going to have var word in words and we’re going to append format this time.

So maybe I want a nice, nice append format here.

So I’ll have the list item.

Then I will insert the actual text and then the closing list item tag.

And the list item is going to be the word that we’re using as the iteration variable.

So we’re making a list.

And once again, I can write line stringbuilder, thereby getting our list.

So here is the list of elements.

But you can see that we’re already making too much effort here.

We’re working with a quite a low level interface where iterating through sets of objects.

We would rather this whole thing with HTML was presented in some kind of tree, in some kind of structured

format where you could manipulate this tree, traverse this tree and so on.

And this is in actual fact how things are done in real life.

And that is the reason why you would want maybe some kind of HTML builder so that you can build up different

HTML objects with a nice, good looking and understandable APIs.

Play Play Stop Play Play Play Play Play Play Start Play information alert