So in the previous example we actually looked at how you can build simple bits of HDMI out of strings.
But obviously this approach doesn't really scale if you want to build something large.
So in this case what you would typically do is you would start creating object oriented structures which
actually represent the different parts of the DMO that you're building.
So let's try doing exactly that.
I'm going to begin by making a structure called hastier element.
Now an element is just going to represent a tag like for example the paragraph tag including everything
that's inside the tag whatever that may be.
And it could be a paragraph inside a paragraph for example so here you would give the DML element certain
traits like for example you might want to have a name as well as the text.
If indeed this element contains any text you might also have inner DML elements which I would put into
a vector so a vector of a simple element called elements and in addition you might have other formatting
options or whatever they happen to be like for example you might want to define the indentation size
for example so in then size equals two because we do want the DML to be printed in a very nice way.
So now you might sprinkle your code with a couple of constructors you can have an empty constructor
and just generate an empty one and you can also have a constructor which for example takes both the
name as well as the text so let's have one of those just doing it like this.
And then of course the interesting part is how to print the whole thing.
So essentially you want some sort of API for building up the stem cell element but you also want some
sort of API for actually printing it so typically that would be a function called SDR.
That's how we typically call them and C++ you can specify the indentation level here house starting
with zero because as you drill into the structure and remember we have the situation where an element
can contain other elements as you drill into the structure.
This indentation value is going to increase and you're going to be able to you know just just print
things better.
Now I'm not going to bore you with the actual implementation of SDR because in the context of our discussion
that's not particularly so relevant here is the kind of thing I would do this basically just does the
indentation in a correct way and it does the recursive invocation so that you sort of you go through
each of the sub elements and you do SDR on them with an increased indent value and so on and so forth.
So this gets you the right structure.
Now the interesting thing is how do you actually build up these elements and this is where the build
pattern actually comes into play because you make something called a builder.
So the DML element here is just a utility class for modeling an HMO element in an object oriented way
in C++.
But to have a builder we're going to have a separate structure.
I'm going to go with HDMI builder.
So this structure whether it's going to do is going to allow us to first of all define the root of our
element and then add children in a very simple fashion.
So first of all I'll have the element called the router that's going to be at the top level element
there's always a single element or Typically there is a single element and then we can have it in the
constructor but we're going to do a constructed differently because remember for whoever is working
with the builder it's inconvenient to start working with the sacred element object which is one that
work with strings basically So I'm going to have a female build a constructor which actually takes the
name of the root.
So root on this call name and here we just set the name of the root so we say read the name he calls
with the underscore name.
So if somebody wants an unordered list they just put in a string called UL in here or a string with
a value well and that goes into the name of the root.
OK so now that we have this we might want to add a couple of children to the root Talman So here once
again you make a utility function.
So this might be something like and underscore child which takes a child name as well as child text
and it simply creates a new DML element and adds it to the list of elements so essentially you would
have something like the following You would have an HMO element being constructed a stimulus element
E with the name and the text.
If there is any text and then of course you would say that elements in place back and you would emplace
back.
That's pretty much all that you have to do.
So we now have a builder.
The only thing we want is just like with the HDMI element we might want a function called SDR which
actually returns a string representation of whatever it is that we're actually building.
So we're going to put it down here so that's going to be basically a string SDR Konst and we're going
to return our roots thought as like.
So now I see that concert as well.
Yeah everything is gone so everything is OK.
So this is the SDR function which is going to actually print our HDMI out after we're done working with
it.
So now what we can do is we can use this builder to also generate a list of the different elements like
these elements for example but we can do it in a specially provided API.
So here I would make the builder so I make an email bulled called builder with the root element of an
ordered list and then I would say well let's add a couple of children.
So I would say builder Dauth and child and here I would add a list item with the text Hello.
And similarly I would add a list title with the text world.
And now having made this and notice I'm using kind of object oriented constructs what I can do is I
can see out for example build a dog SDR and this gives me a texture representation of the whole thing
so you can actually run this and see that we are in fact getting what we want to get.
All right so let me just expand this a little bit and we get to see the outcome.
So here it is so an ordered list with items.
Hello world.
We got really nice formatting here but the upside isn't so much the formatting as the fact that we're
using object oriented structures to define everything.
So this idea of because if printing gets us very nice deep indentation and whatnot and we're doing this
through special dedicated components so instead of working for example directly with the HDMI element
which is certainly something that we could do we're actually avoiding it by creating a dedicated component.
And this happens to be the core of the build design pattern so instead of constructing the object in
a single line of what you're doing is using a specialized component which has the special functions
for helping you construct the component.
And then at some point you just call a function to get whatever it is that you've constructed and work
with that.