So one of the improvements that I want to add to our implementation of the builder is something called
a fluent interface so as you can see we've got two child coal's here one after another.
And what if I told you that you can actually do them like this sort of ADD child 1 and then add child
to another.
This is actually quite common in design patterns such as the builder and it's called a flu and interface.
Now to make a fluent interface you simply change the return type of a function from a void into either
a pointer or a reference to the type you're currently in.
So we're in the DML builder so we can return a reference to a demo builder by simply returning star
this and this way what happens after the first child call.
The end result of that call is a reference to a builder and you can take that reference and do another
call of ADD child on top of that and you can sort of continue this to infinity by doing more and more
calls.
Now this is particularly convenient in builders because sometimes the builders you make they want to
kind of do several very similar things.
Or you might want to specify how the chain of build commands can be constructed then you can do this
using this kind of fluent interface.
Now another thing that you can do is you can give a hint to the consumer to the client that's actually
using your API that they should really be using the builder.
And here once again it's really up to you how to do this.
But for example what you could do is you could make a static function inside the female element because
that's actually the thing that we're building up you can make a static function called build which would
return and DML builder.
So you would have a function called build which would take the route name because ROOM.
That's what the mill builder takes as a constructor argument.
And here you would return simply the HMO builder with that argument so you would return route on this
core and they provided to the constructor of a sham builder and notice we don't have to put in the actual
name of the class we're returning here because it's inferred from this.
And as a result what you can do is you can do something like the following so you can see a auto builder
equals and then you can see a similar element.
Colin Colin build so you build the order list for example and then you can call at child on it however
many times you want.
So this is a valid way of actually putting many things into a single basket so to speak.
So at the same time you constructed from the elements you're building up and then you call and child
on the final kind of icing on the cake if you will is that it's possible to define an automatic conversion
operators so that when you instead of making a builder Let's suppose you actually want to get an HMO
element here and let's suppose you want to have an element called element or just L.M. in here as a
result of this build process.
Now we know that the right hand side typically yields an HMO builder because you called Build and then
you call a child all of these things as you can see that return a builder.
So how can you convert one to another.
In actual fact it's it's rather easy so you can simply make an operator call him element where you would
return the roots element that you're building up.
So this is how all of this actually becomes valid.
And then you have the element and you can sort of operate on the element now in some situations if you
know that this operation is going to be the last operation that you ever need to do and you don't care
about what you can also do is you can move the route out of here so you can se the move the routes out
of the function.
But really it doesn't matter in this particular example so much and it's really up to us.
For example if you are using something like a factor instead of a builder it might make more sense.
But here the finding of this operator what this means is that you're returning.
Well in this case with suddenly returning a copy of the operator.
Mind you it's also possible to change this entire API to stop working on references and start working
on pointers in the sense that you can for example make a copy of this function and instead of returning
a reference you can return a pointer and it's just going to call this child 2.
And here you can instead of returning star this you would return this and this way you would have the
whole thing made up of pointers and as a result if you want to build an actual invocation chain of these
add child to calls they would use Dasch greater than in-between instead of the.
So you would have something like the following instead.
And certainly if you want the start of the invocation to also use Dasch greater then then instead of
returning the value of the symbol builder and hoping for some return value optimisation here what you
can do is you can return a static unique VTR for example of an HMO.
So they're called once again build where you would simply to make unique and so on.
So it is really up to you whether you want the reference API or the pointer API I would of course recommend
the reference API just seems more sensible this way.
Now returning an email builder sort of by value in quotes is not a particularly big thing here.
I would say it's not a particularly big problem.
Now the operator is just you know some icing on the cake that you may or may not want to add to your
design but you are sort of giving hints to the user that you want them to construct the object this
way.
Now furthermore what you can do is you can actually force them to do this.
So you can force people to actually use the builder instead of constructing the object via ordinary
constructors.
And the way this is done is fairly obvious as well.
So we have a couple of constructors here and at the moment these constructors are they are public.
But what if I do it like this for example.
And I only started doing the public stuff from right about here.
This way the constructors actually become private and you can no longer actually use them.
Now the problem with this is that not only can you not use them but the HMO bill there.
Unfortunately you cannot use them either as you can see here because they are now all completely private.
So in order to do this the only way and the way that C++ allows you to do this is to specify a friend
class so you say French class system builder and from this point on you're good to go so the client
meaning me as the consumer of the API I can go ahead and just make my own HDMI element.
I kind of just go and say Oh.
Can you please give me an email element's cold because the constructor's on e are completely unavailable
as a result this is not valid so I am resorted to go looking for any kind of API on a element that I
can actually find and of course I'm not going to find any constructor's or any members on it.
But I will find a static function called build.
So for me will be a fairly obvious choice.
I would say DML element.
Colin Colin I would look in here and I would say oh look there is a build here so I would call build
and I see that it takes some room name or other and that would put a dot here.
And typically what you would do you suddenly have your string construction here if you want string representation
or at some point you might want to actually have an explicit kind of build function which kind of finishes
the build so to speak so when you call build that's kind of like the the start.
And you can actually rename them you know what we can actually rename these things.
So for example when you're starting out with a builder you can call it something else so we can for
example go here and rename it to create and when it comes to build that would be something that would
be added once again as an element of a steamboat builder.
So you would have a function which returns an HMO element.
It's going to be called Build and all it does is it returns of the roots of the object that you're actually
building so the consequence of this is if you want to build up an if you want to kind of simulate the
user to actually have this invocation in the right way is the element colon colon create with a particular
root and then you do your ADD child calls and whatever.
But when you're done you call build and build will be the final step because one of the problems is
that even though you have this implicit conversion operator your clients might not know about it.
They might not know that this kind of conversion is possible so they are going to be stuck with a builder
and not knowing how to get the route from the builder and I hear the words is public.
But typically once again what you would do is you would hide it.
You would make it you would make it private typically So typically you would if I make it class you
would hide it and you would only start the public section from here so it wouldn't be accessible directly
but you would be able to provide a special piece of API for actually returning the object that you've
actually built up.
So this is how you can sort of enhance the situation with builder and provide a better and more sensible
API as well as make sure that the builder API is the only API that's actually available for building
the object by simply making its constructors private.