Tuesday, February 6, 2018

Builder Design Pattern and Its Implementation in Java

Builder Design Pattern is one of the creational design pattern as it is used to solve the problem of creating object having high number of arguments while creating the object.




Other creational Design patterns is -
Factory Design patterns
Singleton Design Pattern

Basically this design patter is used when we have lots of variable needs to initialize while we are instantiating object.

What is Builder Design Pattern ?

Builder Pattern is when piecewise object construction is complicated , providing an API for doing it succinctly.


When to use Builder Design Pattern ?


  • Having 10 or more constructor arguments in object is very less productive , so we use builder design pattern there.
  • When we need to piecewise create the object (Piecewise means we can create the object with few arguments only we dont need to pass all the argument of the object).


There are two type of the Implementation of the Builder Interface
1st way

/**
* The Class BuilderDesignPatternDemo.
*/
public class BuilderDesignPatternDemo {
public static void main(String[] args) {
Person p = new PersonBuilder()
.name("Noeik")
.age(25)
.location("India")
.build();
System.out.println(p.toString());
}
}
class Person{
private String name;
private int age;
private String location;
Person(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", location=" + location + "]";
}
}
class PersonBuilder{
private Person p = new Person();
public PersonBuilder name(String name) {
p.setName(name);
return this;
}
public PersonBuilder age(int age) {
p.setAge(age);
return this;
}
public PersonBuilder location (String location) {
p.setLocation(location);
return this;
}
public Person build() {
return p;
}
public PersonBuilder() {
}
}

Explanation

  • We have BuilderDesignOfInterheritanceDemo as a main class where we are initialise the person object
  • then we have Person DTO with getter and setters
  • PersonBuilder is a Builder Class of Person , We here is showing the we are returning the same instance of the builder class in each method except build() , In build method we will return the main Person Object after all the value will be set
Point to be Noted 
In Builder Pattern we always return the Instance of Itself so that we can again all others object 



If you have a Scenario when you need to full the value in Object using Inheritance of two Builders 

/**
* The Class BuilderDesignOfInterheritanceDemo.
*/
public class BuilderDesignOfInterheritanceDemo {
public static void main(String[] args) {
PersonA p = new EmployementBuilder()
.withName("Noeik")
.position("Founder")
.build();
System.out.println(p);
}
}
class PersonA{
private String name;
// Employement Builder will fill the value
private String position;
public PersonA() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return "PersonA [name=" + name + ", position=" + position + "]";
}
}
class PersonBuilderA<T extends PersonBuilderA<T>>
{
PersonA p = new PersonA ();
public T withName(String name) {
p.setName(name);
return self();
}
public PersonA build() {
return p;
}
public T self() {
return (T)this;
}
}
class EmployementBuilder extends PersonBuilderA<EmployementBuilder>
{
public EmployementBuilder position(String position) {
p.setPosition(position);
return self();
}
@Override
public EmployementBuilder self() {
return this;
}
}
Here also we are doing the same thing just using inheritance .



Hope the code will help you in understanding why we use the builder pattern and how to implement it.


Builder Design Pattern Example in JDK

Some of the builder pattern example in Java classes are;
  • java.lang.StringBuilder#append() (unsynchronized)
  • java.lang.StringBuffer#append() (synchronized)

If you have any issue do let us know

Thanks for reading
Noeik

0 comments:

Post a Comment