Introduction To The Fluent Builder Pattern - DZone Java
Introduction To The Fluent Builder Pattern - DZone Java
(https://d2u6dc21frjf6h.cloudfront.net/d/3eJydUMtOwzAQ/BXLh55w8wKVVoqQmwYubUnpIXCKXMdKLBI7xHaLQPw73kgIzuxhpd2dmR3NJx7xCuHW2sGsgoBdzJz17E
n=1)
DZone (/) > Java Zone (/java-jdk-development-tutorials-tools-news) > Introduction to the Fluent Builder Pattern
development-tutorials-tools-news) · Presentation
The fluent builder pattern is one of the most useful patterns, especially when you want to build complex objects. For example, say you want
to build a complex object by initiating the builder, calling the respective setters, and finally, calling the build method. Once the build method
is executed, you will get the desired model/entity/pojo object back.
This looks quite simple but there is one catch. If the object building is complex and there are too many setters methods, then normally the
developer's tendency is to forget some of the setters and build the object. Doing so, many of the important object attributes will be null, and
so, no setters are being called for the same.
In many enterprise applications, there will be a core entity like Order/Loan, and it might get initiated in many sections of the code, missing
the set attribute can be a costly process in terms of development and maintenance. So, what do you do?
The answer is to force the developer to set all required setter methods before calling the build method. Doing so, all required attributes will
get initialized and build object is in your desired state. But how to force the developer? The answer is through the fluent builder pattern.
Fluent builder pattern is a style of coding which force the developer to create the object in
sequence by calling each setter method one after the another until all required attributes are set.
Let’s go in detail about how to achieve the fluent builder pattern. The fluent builder pattern is similar to any fluent API call, but this is used
to build the object. For achieving fluent builder, we are going to create an interface chain where each interface method will return the next
interface type. Confused? Let me explain with an example. For the sake of simplicity, we will try to build the Email Object, which will
contain all the info to send the email.
If you see for each attribute that there is one interface and one method, the return type of the method is the next interface in the sequence.
Creating the builder class is easy, it needs to implement all our interfaces defined as part of the interface chain as follows:
We need to provide the instance method for the builder and make the constructor private so that the developer is forced to create the builder
object as we want. Another important point is that the instance method should return the first interface type in the chain. For any optional
attribute that is required, we need to create methods in the last interface in the chain along with the build method.
Let's create the Email Object with only mandatory and non-mandatory attributes as follows:
Conclusion
If your requirement is to build a complex object for which you want to set the mandatory attributes and avoid making any mistakes, then the
fluent builder will be more useful rather than the traditional builder pattern.
Topics: DESIGN PATTERN, JAVA, FLUENT BUILDER PATTERN, TUTORIAL, CODE, EXAMPLE
Published at DZone with permission of Milind Deobhankar. See the original article here. (https://blogmilind.wordpress.com/2019/05/02/fluent-
builder-pattern/)
Opinions expressed by DZone contributors are their own.
Popular on DZone
Partitioning MySQL: Why, When, How? (/articles/partitioning-mysql-why-when-how?fromrel=true)
ABOUT US
About DZone (/pages/about)
Send feedback (mailto:[email protected])
Careers (https://devada.com/careers/)
Sitemap (/sitemap)
ADVERTISE
Advertise with DZone (/pages/advertise)
+1 (919) 238-7100 (tel:+19192387100)
CONTRIBUTE ON DZONE
Article Submission Guidelines (/articles/dzones-article-submission-guidelines)
MVB Program (/pages/mvb)
Become a Contributor (/pages/contribute)
Visit the Writers' Zone (/writers-zone)
LEGAL
Terms of Service (/pages/tos)
Privacy Policy (/pages/privacy)
CONTACT US
600 Park Offices Drive
Suite 300
Durham, NC 27709
[email protected] (mailto:[email protected])
+1 (919) 678-0300 (tel:+19196780300)
Let's be friends:
(/pages/feeds)
(https://twitter.com/DZoneInc)
(https://www.facebook.com/DZoneInc)
(https://www.linkedin.com/company/dzone/)
https://dzone.com/articles/fluent-builder-pattern 3/4
3/9/2021 Introduction to the Fluent Builder Pattern - DZone Java
https://dzone.com/articles/fluent-builder-pattern 4/4