Ambiguity at Builder Design Pattern -


i have several questions on builder pattern. builder pattern uses several methods constructing instance of class , each method return this return value.

my questions are:

  1. why each method return return type instance of same class?
  2. what benefit of pattern vs using setter method?

you can implement builder pattern without having methods return this. advantage of having methods return instance of same class can chain methods together. example:

return new objectbuilder().withwidget("a").withgadget(5).build(); 

the primary reason use pattern (over setters on constructed object) separate logic building object object itself. typically means built object has no public setters simplifies considerably. might need either complex constructors or protected setters support builder.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -