mercredi 12 août 2020

Builder pattern: When should the model members be final?

The builder pattern is one of the most popular creation patterns, and it has numerous benefits. I specifically want to understand if immutability of the model object itself is one of the key benefits. All the while I thought it was, but I could not find any backing documentation on the same. Consider this scenario, you are creating an object from a network call (from json let's say). We create model objects and it has a Builder inline. This is what everybody does. The members of the model are also private. Since this is a network object, the members won't have setters. My doubts are

  • With builder securing object creation, do we need to make members private.
  • Can we instead keep them public final and eliminate need for getter()
  • In general (irrespective of the above two points), shouldn't all non-settable members be final? I don't see many people making members final, why is it so? Is this a good approach or not?

Aucun commentaire:

Enregistrer un commentaire