Java builders
2015-04-12So it’s time to scrutinize a builder pattern. There are bunch of options to implement builder pattern in Java:
- classic builder;
- elegant builder;
- classic builder with IntelliJ IDEA plugin;
- Google AutoValue builder;
- Project Lombok builder;
- POJO builder;
- Immutables builder.
All of these variants have been examined in Java builders GitHub project.
The winners are (and this a tough IMHO):
- IDEA InnerBuilder plugin if you use builder class from frameworks like Spring/Spring MVC or Play. You can’t pass validation annotations to generated class. Despite everything if there is a way to do this I’ll be happy to mistake.
- Immutables.org builder in all other cases. It’s features rich and generates beautiful code.
But do not forget about AutoValue because it’s backed by Google itself.