top of page
Writer's pictureRohit Modi

Builder Pattern with Lombok Spring Boot

Updated: Dec 18, 2022

Hey Guys, welcome to tech-maze, in today's article, we will see how we can utilize one of the smart feature of Lombok.

Lombok library mostly used to avoid boilerplate code for setter, getter, toString and constructor, but it also provide Builder pattern.


Either we can go and provide all these annotation over the POJO class @Getter, @Setter, @ToString and @EqualsAndHashCode or we can simply use @Data which provide all these features in one annotation.


To get the feature of builder pattern we go for @Builder annotation, but this pattern is not utilized widely by the developers because of that one of its feature has not been used, I ll explain one of the functionality which Builder pattern provide which most of the developer missed to utilize, and its worth to know that.


We can annotate a POJO class with @Builder and then builder pattern will be available for this particular class, but there is a catch, suppose we call an API and it returns 20 attributes, our POJO class contains 21 attribute, 20 attributes mapped to all 20 attributes which has received as response and one attribute we set based on one of the attribute received in the response.


For example suppose we get the details of a country as a response from an API based on the language and response contains country code as IN or US or UK etc., once we receive this, we have condition to set the 21st attribute as countryOrigin based on this value such as if its IN, countryOrigin will set to INDIA, if its US then countryOrigin should set to United States of America etc.


but when we do this with the builder pattern the code will be like following :


Lets say returned response object is builderPatternVO which is BuilderPatternVO class type, then we calculate the countryOrigin based on IN or US etc. and COUNTRY_ORIGIN contains that value such as "INDIA" or "United States of America" which eventually set in the BuilderPatternVO class attribute countryOrigin.


if we use following code thinking that, existing attribute values will intact but in reality, all attributes will loose its values and set to null except countryOrigin


builderPatternVO = builderPatternVO.builder()

.countryOrigin(COUNTRY_ORIGIN)

.build();

OR

builderPatternVO = BuilderPatternVO.builder()

.countryOrigin(COUNTRY_ORIGIN)

.build();

so finally we have to go in the following way, here we can see, we have to set all the attributes again in order to keep these values.


builderPatternVO = builderPatternVO.builder()

.countryOrigin(COUNTRY_ORIGIN)

.attribute1(builderPatternVO.getAttribute1())

.attribute1(builderPatternVO.getAttribute2())

.attribute1(builderPatternVO.getAttribute3())

.

.

.

.

.

.attribute21(builderPatternVO.getAttribute21())

.build();

OR

builderPatternVO = BuilderPatternVO.builder()

.countryOrigin(COUNTRY_ORIGIN)

.attribute1(builderPatternVO.getAttribute1())

.attribute1(builderPatternVO.getAttribute2())

.attribute1(builderPatternVO.getAttribute3())

.

.

.

.

.

.attribute21(builderPatternVO.getAttribute21())

.build();

For a POJO class which have few attributes, this approach is fine but if we have more attributes then this process becomes tedious, time consuming, repetitive and consume more space.


Now the question arise how we can avoid writing repetitive code


and the answer is we can achieve this by twisting our annotation a little bit, instead of @Builder use @Builder(toBuilder = true)


and then following code will work as charm


builderPatternVO = builderPatternVO.builder()

.countryOrigin(COUNTRY_ORIGIN)

.build();



That's all folks, hope this will help at some extent, for more details please have a look in the below GitHub project, I will come up with another article shortly.



A Big thank you for checking out my article, this really encourage me to come up with more topics.


Here are some resources you might be interested ------------------------









202 views0 comments

Comentarios


Spring Boot
bottom of page