krishna
krishna

Reputation: 41

Is builder pattern about number of arguments in the constructor?

I am learning design pattern. As part of that I am going through the Builder design pattern. The definition says that "The intent of the Builder design pattern is to separate the construction of a complex object from its representation". I am trying to understand what does it mean a "complex object".When I check online people are mentioning if the constructor has more arguments or optional arguments then to use builder object.

Is that explanation is correct? If we have optional parameters then why dont we remove those arguments from the constructor and if the client requires that argument can set using a set method?

If we have more constructor arguments and try to create the object using builder class then there can be a chance where client didnt set some arguments and give a call to final method to get the object.

Upvotes: 2

Views: 1432

Answers (2)

Mik378
Mik378

Reputation: 22191

Take the classical example of a Pizza.

In some restaurants, there are different variations of pizza.
One with tomatoes and tuna, the other with tomatoes, tuna AND cheese, another with tomatoes, tuna, cheese and ONE egg in the middle.
It's a complex object in the sense that it potentially allows a huge combination of fields / ingredients.

So regarding a same object, here the pizza, there are several possible representations (variations).

Without a practical way of constructing this object, you would deal with a lot of possible constructors dealing with each variation, a real mess in the Pizza class.

You evoked the fact to get rid of those constructors by using some setters.
But in a good OO design, you want to get instance of a complete object at once through one constructor or ...another fluent way that consists of the ... builder pattern to avoid creating an unfortunate partial Pizza before finishing adding its REQUIRED ingredients.

Besides, setters potentially break several invariants that should be protect by the object itself.
Indeed, what if a code client creates a "Neptune Pizza" while forgetting to add the main ingredient like tuna, it would not be a (valid) Pizza any more.

Simple setters give "power of manipulation" to any client, that shouldn't get it.
While builder object is also responsible to validate the required fields while allowing to omit optional fields.

Builder pattern aims to avoid those "infinite" additions of constructors to represent each pizza by the way of a fluent API that allows easy, incremental and cohesive combination of Pizza's ingredients and THEN returning the consistent and complete object to the client.

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58822

Even if you have 5 optional parameters, how will you construct object without build pattern?

Either have 5! number of constructors that will consist of all parameters options, which is bad practice

Or create with one (or more) constructor and then use setters for setting the optional parameters

But then, how will you know/verify object is fully constructed?

Upvotes: 1

Related Questions