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:
- why each method return return type instance of same class?
- 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
Post a Comment