Method Chaining: In java, Method Chaining is used to invoke multiple methods on the same object which occurs as a single statement. Method-chaining is implemented by a series of methods that return the this reference for a class instance.
Implementation: As return values of methods in a chain is this reference, this implementation allows us to invoke methods in chain by having the next method invocation on the return value of the previous method in the chain.
id = 1, name = Ram, address = Noida id = 2, name = Shyam, address = Delhi
Need of Builder Pattern : Method chaining is a useful design pattern but however if accessed concurrently, a thread may observe some fields to contain inconsistent values. Although all setter methods in above example are atomic, but calls in the method chaining can lead to inconsistent object state when the object is modified concurrently. The below example can lead us to a Student instance in an inconsistent state, for example, a student with name Ram and address Delhi.
Output may be:
id = 2, name = Shyam, address = Noida
Another inconsistent output may be
id = 0, name = null, address = null
Note : Try running main method statements in loop(i.e. multiple requests to server simultaneously).
To solve this problem, there is Builder pattern to ensure the thread-safety and atomicity of object creation.
Implementation : In Builder pattern, we have a inner static class named Builder inside our Server class with instance fields for that class and also have a factory method to return an new instance of Builder class on every invocation. The setter methods will now return Builder class reference. We will also have a build method to return instances of Server side class, i.e. outer class.
Output is guaranteed to be one of below:
id = 1, name = Ram, address = Noida
id = 2, name = Shyam, address = Delhi
The Builder.newInstance() factory method can also be called with any required arguments to obtain a Builder instance by overloading it. The object of Student class is constructed with the invocation of the build() method.
The above implementation of Builder pattern makes the Student class immutable and consequently thread-safe.
Also note that the student field in client side code cannot be declared final because it is assigned a new immutable object. But it be declared volatile to ensure visibility of shared reference to immutable objects. Also private members of Builder class maintain encapsulation.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- LongStream.Builder build() in Java
- DoubleStream.Builder build() in Java
- IntStream builder() in Java
- LongStream builder() in Java
- Stream builder() in Java with Examples
- DoubleStream builder() in Java with Examples
- Stream.Builder accept() method in Java
- Stream.Builder add() method in Java
- IntStream.Builder add() method in Java
- Stream.Builder build() in Java
- DoubleStream.Builder add(double t) in Java with Examples
- IntStream.Builder build() in Java with Examples
- LongStream.Builder add(long t) in Java
- LongStream.Builder accept() method in Java
- IntStream.Builder accept() method in Java
- Locale.Builder clear() method in Java with Examples
- Locale.Builder setLanguage() method in Java with Examples
- Locale.Builder setExtension() method in Java with Examples
- Locale.Builder setUnicodeLocaleKeyword() method in Java with Examples
- Locale.Builder setLanguageTag() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.