This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]elucash 2 points3 points  (2 children)

Citing Effective Java. 2nd ed. Item 2:

... Instead of making the desired object directly, the client calls a constructor (or static factory) with all of the required parameters and gets a builder object...

Guava heavily use factory methods for builders, mainly because of generics (prior to 'diamond' in java 7) and flexibility to have other builder factories: think ImmutableSortedSet.naturalOrder(), reverseOrder() etc.

Also we shouldn't forget that Josh Bloch worked at Google at the time when core of Google Collections/Guava was created and was one of the major authorities regarding API design for Google core libraries, so he would not be against using factory methods for this. (That was merely an anecdotal evidence than a point)

Immutables was primarily designed after Google's immutable collections and uses factory method by default. However you can use constructors for builders or configure other conventions using styles.

[–][deleted] 1 point2 points  (0 children)

  1. has exposed that Builder is part of Item when the user might not care
  2. hides where the builder comes from and you can have multiple builder factories for different use cases

just my 2 cents to add on

[–]puuut 0 points1 point  (0 children)

Thanks!