2

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.

krishna
  • 41
  • 4
  • If you have 5 optional parameters, how will you construct object without build pattern? – Ori Marko May 24 '20 at 09:21
  • I might be wrong.. I will have my constructor with only mandatory parameters then after creation of the object I will set the values of optional parameters using Set Method .After that I will using the object . – krishna May 24 '20 at 09:31
  • @krishna +1 for a Good question. Its almost the same with setters but the difference is that with setters you have an object created and references can refer to it and do operations. Using Builder pattern you ensure of collecting all the states before creating the object. The object returned to the client has all its states set. Usually all the states are mandatory and the states we say as optional have a default value which is taken care by the builder and client code doesn't need to care for them if non default are not needed. – nits.kk May 24 '20 at 09:42
  • 1
    Also The properties are usually set once only and if we have setters then those can be again set. Builder pattern lets u not set them again. You can go through the answer https://stackoverflow.com/a/6927730/504133 for more details. – nits.kk May 24 '20 at 09:46

2 Answers2

1

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?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

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.

Mik378
  • 21,881
  • 15
  • 82
  • 180