Lecture thumbnail 0:00 / 0:00 So we’re going to begin the discussion of design patterns with the builder design pattern.
Our first creational design pattern.
So what is the motivation behind a builder?
Well, the thing is that some objects are simple, and if you have a simple object like a point which
has X and Y coordinates, you can go ahead and create this object in a single constructor call.
You just call the constructor, provide the arguments and that’s it.
On the other hand, other objects require a lot of ceremony to create.
So, for example, when you are constructing a string from a dozen different pieces, what you typically
want to do is not call a constructor and not do a concatenation where you have 11 plus signs.
Instead, you make a string builder which actually lets you construct an object piece by piece.
So having an object with ten constructor arguments is really not productive.
It’s not a great way to go because people make mistakes when they see too much variety.
If you have a constructor with ten arguments, you may miss up two of the arguments by misplacing one
of them.
And once again, code completion in modern Ides is great, but it’s not that great and sometimes it
can give you a false sense of security in a way.
So having a massive constructor is not a great way, and instead you should opt for piece wise construction.
So allow people to construct objects piece by piece and provide a good API for actually doing it.
And this is what the builder pattern is all about.
So a builder is essentially a separate component.
So when piece wise construction of an object is complicated, then you provide a special API for actually
doing it succinctly.
Play Stop Play Play Play Play Play Play Play Start Play