Builder Design Pattern

Type : Creational Design Pattern

Summary

Builder pattern is a Creational Design Pattern and solves the problem of creating complex objects or objects whose constructor needs many parameters.

Details

Considering a Class whose constructor has a number of parameters, we have to send all of them to instantiate an instance, which could be error prone and complex sometimes.
Also if the constructor has some optional parameters or if we want to have some parameters to have fixed value, we might end up with a number or Constructors for the object. Builder pattern solves this problem by exposing a builder object which can have multiple methods based on different parameters.

Example

For our example we will consider a Class BlogObject which represents a Blog and has a number of fields. Some of the fields like, Tag, Summary etc are optional. Without Builder we might need to have a number of constructors for the Class. But with Builder we can create the instance of the Object easily and efficiently.

Class Diagram



Sample Code


// blog object
public class BlogObject {
  private String blogURL;
  private String blogName;
  private String postName;
  private String postSummary;
  private String postContent;
  private String authorName;

  // static Builder Class
  static class BlogObjectBuilder {

    private String blogURL;
    private String blogName;
    private String postName;
    private String postSummary;
    private String postContent;
    private String authorName;

    public BlogObjectBuilder blogUrl(String blogUrl) {
      this.blogURL = blogUrl;
      return this;
    }

    public BlogObjectBuilder blogName(String blogName) {
      this.blogName = blogName;
      return this;
    }

    public BlogObjectBuilder postSummary(String postSummary) {
      this.postSummary = postSummary;
      return this;
    }

    public BlogObjectBuilder postContent(String postContent) {
      this.postContent = postContent;
      return this;
    }

    public BlogObjectBuilder authorName(String authorName) {
      this.authorName = authorName;
      return this;
    }

    public BlogObject build() {
      return new BlogObject(this);
    }

  }

  BlogObject(BlogObjectBuilder builder) {
    this.blogURL = builder.blogURL;
    this.blogName = builder.blogName;
    this.postSummary = builder.postSummary;
    this.postContent = builder.postContent;
    this.authorName = builder.authorName;
  }

  @Override
  public String toString() {

    return new StringBuilder("").append("Author Name=").append(this.authorName).append(" ")
        .append("Blog Name=").append(this.blogName).append(" ").append("Blog URL=")
        .append(this.blogURL).append(" ").toString();
  }

}


public class BuilderClient {

  public static void main(String[] args) {
    BlogObject bo = new BlogObject.BlogObjectBuilder().blogName("JavaExp Blog")
        .blogUrl("javaexp.blogger.com").build();
    System.out.println(bo.toString());
  }
}

Advantages

Reduces the number of constructor by using another object.
Increases the readability of the code and less error prone in passing parameters of same type to the constructor.

Disadvantages

Code duplication as Builder Class needs to copy all the fields from the target object.

References

Comments

Post a Comment

Popular posts from this blog

Converting Java Map to String

Difference between volatile and synchronized

Invoking EJB deployed on a remote machine