Why is the Builder template better than the constructor with arguments in the created class object?

Why can't we do different assembly steps inside the constructor itself. if the assembly steps take arguments, why they cannot be provided as arguments to the constructor and used in the constructor to create the object.

AFAIK, in the Builder template, a client that creates a specific object; then what is the advantage of using a constructor instead of a constructor with arguments in the created class object?

+6
c ++ builder-pattern
source share
1 answer

ABOUT! I understood. I looked at the Wikipedia example and realized why Builder is useful. This is useful when the client does not know what arguments are passed to the constructor, since it is very complex and, therefore, cannot directly call the constructor and get the object. Therefore, he asks for help from the Concrete Builders, who know what arguments are passed to the constructors and, therefore, get the created object.

In principle, if the client is the one who is basically going to pass arguments to the constructor of the class whose object is created, then Builder is not so useful. It might be better to use a prototype. On the other hand, if there is a small finite set of specific objects that can be created from a class, passing arguments to the constructor (or calling setters) of this class, and if they are the ones that are often used, then it is better to encapsulate this argument passed to Builder class, and use them to create objects for you.

+5
source share

All Articles