Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Good article. Simple, lucid & very specific. Brings all the initialization complexity to inner class, which keeps you outer class clean, love this pattern. Is it like builder pattern cannot be implemented on those class whose attributes keep shifting from mandatory to optional and vice-versa, unless its designed to take every attributes thru the setter method of inner class.
- HIMANSU NAYAK
Good article. Why does below functions needs to return “this” when setting parameter? can you please give pointer on this?
- M
Hello Pankaj i have Question Why u have used only static nested class why not normal/regular inner class…i knw differences but in this example why we have used static nested clas only…plz reply…!
- Amritpal Singh
Hi Pankaj, There is still an issue with this approach. We are still expecting the client code to invoke the setters explicitly to ensure that the object state is consistent before the finally created object is returned upon invocation of build() method. We could have invoked the build method without even calling the setters and the boolean variables would have been initialized to false by default and still the object creation would have been successful with the consistent state. I think the another approach could be to provide a series of overloaded constructors with various combinations of optional parameters which invoke them in sequence until all the parameters are set and then the final object is created. In this case, we would not require to have even a build method. Please comment.
- Ashish
Can you please share/post UML design for the same as explain above Thanks Ashakant
- Ashakant
What is a need of getter and setter methods for outer class in above example if we are providing private constructor and no outer world can access the outer class?
- Rishi Naik
By this way, we can also create the object. Can you please explain the difference in using return this and the way below : public class TestBuilderPattern { public static void main(String[] args) { // Using builder to get the object in a single line of code and // without any inconsistent state or arguments management issues Computer.ComputerBuilder comp = new Computer.ComputerBuilder(“500 GB”, “2 GB”); comp.setBluetoothEnabled(true); comp.setGraphicsCardEnabled(true); comp.build(); System.out.println(comp); } public void setGraphicsCardEnabled(boolean isGraphicsCardEnabled) { this.isGraphicsCardEnabled = isGraphicsCardEnabled; // return this; } public void setBluetoothEnabled(boolean isBluetoothEnabled) { this.isBluetoothEnabled = isBluetoothEnabled; // return this; } }
- vijay
This blog was really nicely articulated and helpful. Thanks …
- Arijit Medya
Builder pattern described here is completely different from what other websites implementations. Here it shows with static inner class but others its not. Not sure which one is correct.
- Prabhakaran
I have one question that we are returning this in set() I know it is very important to return this in Builder pattern. But, Can you tell me what is the actual meaning when we return this and how it is actually executed. I have some confusions that how complier deal when we have used set() method of different fields for multiple times in same line of code while creating object and who will pick up this references of object before build().
- Kishan