DEV Community

Umair
Umair

Posted on

Why should we use Lombok's @Builder annotation ?

Have you also ever looked at the long list of getters and setters methods in an Object class, like this User class (for example)

User class with lots of getters and setters

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:

Creating a User class object using setters

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:

Creating a User class object using Builder

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:

Converting User object to it's Builder using toBuilder()

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

Top comments (2)

Collapse
 
siy profile image
Sergiy Yevtushenko

Builder is one of the most frequently misused patterns. In many cases FluentBuilder is much better solution.

Collapse
 
umr55766 profile image
Umair

Thanks a lot @siy for reading and giving your feedback. Your approach is really very insightful, loved it !!