I recently questioned if it is okay to use a builder to make multiple objects. The software was designed in such a way that this is possible, but I hesitated because I did not know if it was a good idea.
I know for a fact that a lot of builders in our software, might not be suitable for this use case, as they have too many dependencies (that shouldn't be there) on using the proper sequence.
That said, the following quotes are good to take to heart when designing builders1 2:
“The Builder pattern is flexible. A single builder can be used to build multiple objects. The parameters of the builder can be tweaked between object creations to vary the objects. ”
“The builder object is responsible for constructing a valid object but the object is not constructed until you call the build() method. This means the same builder can be used multiple times to construct completely different objects.”
I especially like the last quote.
References
- [1] StackOverflow - How to use a single builder to build multiple objects?
- https://stackoverflow.com/questions/14429043/how-to-use-a-single-builder-to-build-multiple-objects
- [2] Effective Java 2
- Joshua Blog
No comments:
Post a Comment