Builder Design Pattern in JAVA

Design Pattern a pretty fancy name given but indeed it has a lot of fancy stuff inside it. Lets explore a bit in detail.

Design Patterns are based on an important principle “Program to an interface and not an implementation”. There are a lot of Design Patterns but we will be discussing the following patterns: Builder Pattern, Factory Pattern and Singleton Pattern.

BUILDER DESIGN PATTERN:

When to use ?

Imagine a class with a lot of parameters using either a constructor or Java Beans conventions to set all these parameters where some of the parameters are optional and others required. Hence, we have been using the below way but it doesn’t allow the end-user to leave the optional parameters unset and at the same time doesn’t even force the end user to always pass the required parameter.

Problem with the constructor approach: we will have to set all the parameters defined in the signature irrespective of whether it’s optional or not.

Problem with the Java Beans approach: we will have the flexibility to leave the optional parameters unset but it won’t force us to always set the mandatory parameters. Hence, a possibility of run time error.

public class WithoutBuilderPattern {

private String name;        // Required Field

private int age;                   //  Optional Field

private int id;                      //  Optional Field

private String role;           //  Optional Field

private String company; //  Optional Field

private String area;          //  Optional Field

private int areaCode;      // Required Field

// Initialising all of them in constructors:

public WithoutBuilderPattern(String name, int age, int id, String role, String company, String area, int areaCode) {

this.name = name;

this.age = age;

this.id = id;

this.role = role;

this.company = company;

this.area = area;

this.areaCode = areaCode;

}

// OR Java Bean Approach:

public void setName(String name)

this.name = name;

}

public void setAge(int age) {

this.age = age;

}

public void setId(int id) {

this.id = id;

}

public void setRole(String role) {

this.role = role;

}

public void setCompany(String company) {

this.company = company;

}

public void setArea(String area) {

this.area = area;

}

public void setAreaCode(int areaCode) {

this. areaCode = areaCode;

}

Advantage of Java Beans Approach:

  1. Easier to read and maintain.

Disadvantage of Java Beans Approach:

  1. Object of this class will be in a consistent state when all the setters have been called. However, imagine setters have to be called again and only half of them have been called, here this object becomes inconsistent if without setting all of them, a get operation is done. This is because, half of the values in the object will be the new one and the rest half will correspond to the previously set values.
  2. Objects are mutable that is the values can be changed using setters.
  3. It doesnt force users to set mandatory values or doesnt allow users to leave optional values.

 

Solution: Using Builder Pattern

public class WithBuilderPattern {

private String name;        // Required

private int age;                  // Optional

private int id;                     // Optional

private String role;           // Optional

private String company; // Optional

private String area;          // Optional

private int areaCode;       // Required

public WithDesignPattern(Builder builder) {

this.name = builder.name;

this.age = builder.age;

this.id = builder.id;

this.role = builder.role;

this.company = builder.company;

this.area = builder.area;

this.areaCode = builder.areaCode;

}

public String getName()

return name;

}

public int getAge() {

return age;

}

public int getId() {

return id;

}

public String getRole() {

return role;

}

public String getCompany() {

return company;

}

public String getArea() {

return area;

}

public int getAreaCode() {

return areaCode;

}

public static class Builder {

private String name;        // Required

private int age;                  // Optional

private int id;                     // Optional

private String role;           // Optional

private String company; // Optional

private String area;           // Optional

private int areaCode;       // Required

// Builder constructor only with the required Parameter

public Builder(String name, int areaCode) {

this.name = name;

this.areaCode = areaCode;

}

public Builder age(int age) {

this.age = age;

return this;

}

public Builder id(int id) {

this.id = id;

return this;

}

public Builder role(String role) {

this.role = role;

return this;

}

public Builder company(String company) {

this.company = company;

return this;

}

public Builder area(String area) {

this. area = area;

return this;

}

public Builder build() {

return new WithDesignPattern(this);

}

} // close of static class Builder

} // close of main class WithDesignPattern

Advantage of Builder Pattern:

  1. Object of this class will be in a consistent state irrespective because object will be obtained in a single statement (with all mandatory parameters and optional parameters set). Therefore, the issue presented in Java Beans concept is resolved with Builder
  2. Objects are immutable that is the values cannot be changed using setters because it requires a builder object to change the value.
  3. Code is readable

Disadvantages of Builder Pattern:

  1. Increased Lines of Code.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s