Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Learn more Got it

Java J2ee development experts explaining use of builder pattern

Published 5 years ago

In this post, Java j2EE development experts explain the use of Builder pattern for constructing intricate immutable objects. Understand the references shared by professionals in this post and use builder pattern for java development project.

Introduction

In Java, we know that how an immutable and mutable objects behave and how it can be constructed. Builder pattern is useful for constructing complex immutable objects. I.e. an immutable object which having too many parameters in constructor, it is always a tedious job for the caller to initialize that object. Here the builder pattern comes into the picture.

A creational design pattern that is used to separate the construction from the representation. Java.lang.StringBuilder class is one of the example for builder pattern.

GOF states that – “Allows for object level access control by acting as a pass through entity or a placeholder object”

Builder – provides an interface to build a Product

ConcreteBuilder – provides an implementation to builder in-order to build a Product

Director – constructs the builder object using thru Builder interface

e.g.

//immutable class with too many parameters in constructor

public final class User {

private int id;

private String firstName;

private String lastName;

private int age;

private long mobile;

private String email;

private String city;

private String state;

private String country;

private long pin;

public User(int id, String firstName, String lastName, int age, long mobile, String email, String city, String state, String country, long pin) {

this.id = id;

this.firstName = firstName;

this.lastName = lastName;

this.age = age;

this.mobile = mobile;

this.email = email;

this.city = city;

this.state = state;

this.country = country;

this.pin = pin;

}

// all gettter methods no setters

}

//simple builder implementation which contains build parts of the user and a buildUser method in order to build user object

public class UserBuilder {

private int id;

private String firstName;

private String lastName;

private int age;

private long mobile;

private String email;

private String city;

private String state;

private String country;

private long pin;

// each method will return this builder object

public UserBuilder id(int id) {

this.id = id;

return this;

}

public UserBuilder firstName(String firstName) {

this.firstName = firstName;

return this;

}

public UserBuilder lastName(String lastName) {

this.lastName = lastName;

return this;

}

public UserBuilder age(int age) {

this.age = age;

return this;

}

public UserBuilder mobile(long mobile) {

this.mobile = mobile;

return this;

}

public UserBuilder email(String email) {

this.email = email;

return this;

}

public UserBuilder city(String city) {

this.city = city;

return this;

}

public UserBuilder state(String state) {

this.state = state;

return this;

}

public UserBuilder country(String country) {

this.country = country;

return this;

}

public User buildUser() {

return new User(id, firstName, lastName, age, mobile, email, city, state, country, pin);

}

}

public class Client {

public static void main(String aa[]) {

UserBuilder userBuilder = new UserBuilder;

User user = userBuilder.id(1)

.firstName(“xxxx”)

.lastName(“yyyy”)

.age(25)

.mobile(9876543210)

.email(“sample@aegis.com”)

.city(“Chennai”)

.state(“TN”)

.country(“India”)

.pin(600001);

.buildUser();

}

}

Conclusion

Just think that, if we didn’t implement UserBuilder, How would we construct the User?

At everywhere, we should directly call the constructor by passing all the arguments to it!

Possible wrong things that may happen,

– Values can be swapped or wrongly assigned since there are lot of similar type of arguments

– Must pass a null argument even the value is not necessary to construct the object

It is actually very clumsy to construct a complex immutable object at directly.

Builder Pattern actually helps to,

– Improves the code readability

– No more passing null values, we could simply ignore the call

– No more possibilities to swap values

– Simply constructs and gives a complex immutable object

Java j2EE development experts have explained the areas where builder pattern is used and help developers. Still, if there is any query in your mind, you can ask freely via comments.

Read more

James Warner

James Warner is an highly skilled and experienced software and mobile application system development manager at NexSoftsys. He has wide experience in IT industries to develop creative business system based on Java, .Net, Python, iOS, Magento and Android. His broad technology knowledge inspire him to face complicated projects. He is working with Healthcare industries, Telecommunications, Banking and finance IT sector to produce high security and user friendly business system.

Got wisdom to pour?

500-

Tags

Java Development, Java J2EE Development
Tweet
2
Like deMilked on Facebook
Want more milk?
Hit like for a daily artshake!
Don't show this - I already like Demilked