Builder design pattern


This is another creational design pattern. Just think about the StringBuilder. If you create a StringBuilder object, you can append strings one by one to the same StringBuilder object.

                StringBuilder stringBuilder = new StringBuilder();
       stringBuilder.append("Java").append("Foundation");

This is the functionality of the builder pattern. There are a few ways to implement the builder pattern.

Using constructor




Using inner class


The second method is the best way to implement the builder pattern. 

Advantages of the Builder pattern

  • The builder pattern solves the complex Telescopic constructors
  • Make it easy to create objects that has many attributes.

Disadvantages of the Builder pattern

  • Code looks complex
  • Object creation depends on the Builder class

Use of Builder design pattern in Java core

  • StringBuilder
  • StringBuffer
  • JsonObjectBuilder
  • Calendar in Java 8
  • Locale in Java 7
  • Stream API in Java 8

Builder design pattern Builder design pattern Reviewed by Ravi Yasas on 8:45 PM Rating: 5

No comments:

Powered by Blogger.