Why is Builder pattern better than a Constructor with arguments in the Class's object being created?
Asked Answered
B

1

7

Why can we not the different build steps within the constructor itself. if the build steps take arguments why can't they be provided as arguments to constructor and utilized within constructor to create the object.

AFAIK, in Builder pattern, the client which specific object to create; then what is the advantage in using a builder instead of a Constructor with arguments in the Class's object being created?

Bungalow answered 4/9, 2010 at 7:16 Comment(0)
B
6

Oh! I get it. I was looking at the Wikipedia example and realized why Builder is helpful. It is helpful, when the client does not know which arguments to pass to the constructor as it is very complicated and hence cannot call the constructor directly and get the object. Consequently, he asks for help from the Concrete Builders who know what arguments to pass to constructors and hence get the object created.

Basically, if the client is the one who is mostly going to be passing the arguments to the constructor of the Class whose object is created, then Builder is not that helpful. It is perhaps better to use the prototype. On the other hand, if there is a small finite set of specific objects that can be created from the class by passing arguments to the constructor (or calling setters) to that class and if they are the ones that are frequently used, then it better to encapsulate this argument passing thingy in the Builder class and use them to create the objects for you.

Bungalow answered 4/9, 2010 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.