This is an example of the builder pattern diagram, so basically here we have the director class, which
in our example is the object itself, the user accounts class, the user account for the only website,
and it will have a reference to a builder class, which in our example will be coded as a static class.
It can be also in the interface, but we will use it as an inner class and that's builder class will
basically build without using complex constructors will basically build our our object.
So let's dive into the code.
Now.
Let's analyze first the all I saw account object, so basically we have the same properties that we've
seen before, but in addition to this, we have a static inner class like we see here, a builder,
which has also the same the same properties as the the outer class.
Right.
But you will see the difference.
So here we also have a constructor for this builder that accepts Nidhi.
We decided to keep the idea in the constructor because the is pretty important.
We don't want to allow the possibility of creating a user account without an idea.
This may crash the application.
So it's also about practice.
So we will keep this one.
This this property here in the constructor and for the rest of the properties we have Setas, we have
defined centers that will return the object of this this builder.
So we have a center for the name, for the address, for the budget and for the discount.
And you can see that the probability of supplying wrong data is decreasing.
Right?
It's significantly decreased because you can see in the name of the method what type of parameter you
need to supply so that this will lower the chances of making some some mistakes.
And the final method in this builder in our class is the build method.
So this will basically the build.
This will build an online storico object and it will assign to each parameter the corresponding parameter
for from the builder class, and it will also return the object itself.
You can see that I have made the constructor of the online store account class private because we don't
want someone that needs to create an online store account in the code to use the constructor of the
object.
We want to use the builder class instead.
Right.
So with the method.
So also we have getters and setters, but these are not important because the actual getters and setters
are inside the builder class here.
So let's see this in action so we can see now that the the code that resulted from applying this builder
pattern is a lot more stable.
It's more readable, it has better readability.
You can easily understand, even if you're not such a technical person, even if you show this code
to someone that is non-technical, she or he will know what this what this means.
Right.
So we create an online store account with the name John Smith, and here we invoke the builder constructor,
which requires an ID.
So we supply ninety here and then we create we assign a name and address, a budget and also a discount
using this pattern, using the builder class and also at the end we supply to build a method that will
return the object that we created.
The object of this type on my account.
So let's see if this has done what it needed to do.
So basically the system of print can and should output the name of this object and therefore we will
see that the name was correctly stored in the object itself.
So let's validate this.
So you can see that the name is correctly outputted here, so everything went well and there are no
issues.
The properties were set as expected.
That's it for the builder pattern.
Join me in the next story where we are going to discuss about the factory pattern.
So see you in the next tutorial.