This content originally appeared on DEV Community and was authored by Umair
Have you also ever looked at the long list of getters and setters methods in an Object class, like this User class (for example)
and got a gut feeling that all these boilerplates are useless. There must be a better way to achieve similar functionality ?
Well, we were correct!
What to expect out of this article ?
In this blog, We’ll be discussing what Lombok's @Builder annotation is and why we need it in the first place? Bit about how we can use it.
Why am I writing this ?
This is to set the context for next article. In which I'll be sharing with you a recent learning which we had in our project, of course, related to this Lombok's @Builder.
Before diving into this annotation, let’s review what Lombok is.
What
Recently, I have started working in Java and that’s how I came to know about Legendary Lombok. For those people who haven’t encountered it yet (like me, until recently) -
Lombok is a Java library. Easily integratable with our IDE, build tools. It provides getter, setter, constructors, few other default functions like equals, etc. We just have to use it’s annotations. It works out of the box.
@Builder annotation enforces Builder Design Pattern. It provides APIs to build objects of the annotated class. It also provides an option to convert existing objects into it’s builder, so that we can modify and rebuild it.
Why
If we have to build object of the User class shown above, (without using Builder) we’ll be doing something like this:
This is still okaish, we’re just setting each of the properties of the user class object. What's more annoying is that there is a long list of getter and setter methods in the User class. Which are nothing but boilerplates. So kinda frustrating.
How
This is where @Builder annotation of the Lombok comes into picture. If we just annotate our User class with @Builder then all the getter setter boilerplates from our User class will be striped out. The Object creation would get simplified something like:
You see, how intuitive it becomes ? It’s just like assigning properties value and building it.
So this is how we create User objects from a builder or raw values. But, what if we want to convert an existing object to it’s builder equivalent.
For that purpose, @Builder provides a configuration parameter toBuilder. This is how we configure and use it:
That's a quick intro about Lombok, @Builder and it's toBuilder.
Watch out for next article regarding our recent learning experience.
Just to ignite the curiosity in you, so we we're modifying an object exactly similar to one shown in above example. As a result, instead of updating same object it was behaving bit weirdly. That's how we uncovered an interesting part of this. Which we'll be covering in next article.
Looking forward to your feedback
This content originally appeared on DEV Community and was authored by Umair
Umair | Sciencx (2021-07-26T19:35:42+00:00) Why should we use Lombok’s @Builder annotation ?. Retrieved from https://www.scien.cx/2021/07/26/why-should-we-use-lomboks-builder-annotation/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.