Builder Pattern examples make the
Builder an inner class of the object it builds.
This makes some sense since it indicates what the
Builder builds. However, in a statically typed language we know what the
On the other hand if the
Builder is an inner class, you should know what class the
Builder builds without looking inside of the
Also, having the builder as an inner class reduces the number of imports since it can be referenced by the outer class--if you care about that.
And then there are practical examples where the
Builder is in the same package, but not an inner class, like
StringBuilder. You know that the
Builder should build a
String because it is named so.
That being said, the only good reason I can think of for making a
Builder an inner class is that you know what the class'
Builder is without knowing its name or relying on naming conventions. For example, if
StringBuilder was an inner class of
String I probably would have known it existed sooner than I did (speculative).
Are there any other reasons to make the
Builder an inner class or does it just come down to preference and ritual?