Before we look at the builder design pattern I wanted to show you where this idea of having a dedicated
builder comes from so let's suppose that you're working on some sort of back and web servers something
and a web server is something that simply serves a certain amount of HDMI over the wire.
So lets suppose that you decide to simply make strings that you're going to return.
Now if your string is simple then it's very simple to create.
So for example if you want a paragraph with a particular provided piece of text that's rather easy.
So I can have a piece of text like for example hello and then if I want to build a paragraph out of
it.
Typically what I would do is I would maybe make a string less code output.
I would add to the output.
The opening paragraph tag so this has an HMO tag.
Then I would add maybe the actual text that I'm interested in.
Then I would have the closing tag.
So slash like so.
And then finally when I'm done I would may be printed somewhere so I might want to for example see out
of the outputs or you know if this was a real web server I would probably return it out of some function
there by sending it to whoever is actually connecting to the web server.
So this is fairly simple stuff.
Unfortunately this approach of taking a string for example and just adding elements to that string it
works.
I mean here is the output we get the hello here but this approach doesn't really scale so let's suppose
that instead of let's say a paragraph you want to print a list of items so you have a bunch of words
and you might have a word Hello.
And the word world like so and you wanted to have them has a bunch of list elements so what would you
do here all you probably wouldn't go for string concatenation exactly.
I mean it works ok but you can use something smarter like an output string Stream.
Let's call it OSX and here you would add maybe the opening tag of an on the order list and then you
would go through all of the words so for auto w in words you would add that word with the appropriate
tags so you would say oh x and then a bunch of spaces for indentation you would have a list item then
you would specify the actual element and then the closing the closing list item tag like so and after
you would add this of course you would also have the closing tags so you would have access closing an
ordered list item and then you would once again print everything using the SDR function.
So here you could for example see out o ss dot SD or something like that.
So essentially this is a better approach here getting some structure in here but unfortunately it's
still this idea of putting a string together piece by piece.
Here is the output you might want to add a couple of line breaks here and here if you wanted to but
apart from that it's all fairly obvious stuff.
Now as you probably know HDMI out is actually a structured format so in HDMI you have paragraphs and
you have lists with list items and you have images and stuff and it also defines certain rules as to
what kind of tag can contain what other kind of tag.
None of this is being enforced here and none of this is being presented here in object oriented fashion
so the idea of the builder design pattern is that instead of let's say building together a string out
of just these little concatenation is what you do is you just define some sort of object oriented structure
which supports its transformation to for example a string and that's exactly what we're going to take
a look at as we actually build the builder design pattern.