When we have a class with lots of fields out of which some are mandatory and some are optional, then it is a bad practice to use Constructors because of several issues, one being poor readability. Builder Design Pattern could be one solution where we separate the construction logic and use a step-by-step approach to create an Object. My question is:
1.)Why can't we Setters in combination with a parameterized constructor which will contain mandatory fields instead of Builder Pattern? The optional fields can be set using setters.
2.) Why should we use the Builder Design Pattern over Setters with the parameterized constructor? One reason I could think of is Immutability. What other benefits does it have?
3.)If we don't want immutability, then is it justifiable to use setters with Parameterized constructors instead of the Builder Pattern?
samedi 12 juin 2021
Using Parameterized constructors(with mandatory fields) and setters instead of Using Builder Design Pattern
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire