I have seen around 6 videos to understand this concept, this is best video which explains this concept in simple and easiest way.......thankssssssssss a lot
My real time example for this is, I have a table and I have a pojo for that table. This pojo is getting used in multiple projects If a column I'm adding for 1 prj sake I have to change pojo constructor and since we are changing constructor where and all prjs we use this need to be changed or we need to create 2 constructors in pojo So this the best solution here is builder design pattern
You are literally very good at explaining complicated topics by giving simple examples. I have searched the entire online courses out there for weeks and here is the only channel that explains this topic very well. You should make a whole design pattern courses. I would pay for that.
Hi Navin, It's a great video, but my question is that, as per your example, what is the need of phone builder, the same can be done with phone class itself, having default constructor and all setter method. Can you please explain.
Having setters prevents you from ever making your class immutable i.e the value of its parameters cannot change. Also there is no guarantee that an object is stable after a setter method i.e it is possible that an object requires at least 3 parameters to function but your object would still exist even after just 1 setter call. You can ensure that all the required parameters are set when u call the getPhone() or build() method, you can even set some of the parameters as final since you are basically calling the constructor to the class making it immutable. Also it is less verbose than having many setters.
How I understood this builder pattern is => for example in your eCommerce application if we search laptop it will show so many results but by using the filter option if u set the ram> 4GB then it will give all the laptops which ur having more than 4 GB ram irrespective of the company.
Good video , but I have a question, what's the need of Phone class after we create phone builder ?! , we can override a toString method in PhoneBulider class and do not use Phone object .
Why not use a non parameterized constructor in Phone class and use setters and getters in main method to pass value. In enterprise application, where we have 20-25 properties in one class and each class internally has 20-30 properties, I don't think this design pattern will work. So is it of any use in enterprise applications??
I didnt get the correct use of builder design patter by adding additional class PhoneBuilder we can also use getter and setter i Phone class itself and use any setter method whichever value its present it will set? Please help to resolve this issue?
Hi Navin, why are you having same variables in both classes Phone and PhoneBuilder. (Not sure if you noticed). In real world we dont duplicate the data.
Hello Sir, Thanks for making the video. I have one question regarding this. We can make one parameter constructor to get the one value like you are doing. Suppose we need to set the price of phone so we can just create a constructor which take price as a parameter and set it. Why we are using the design pattern in this concept?
In our case we had just five parameters, so we can create different constructors with varying parameters like first constructor with 5 parameters, second constructor with 4 parameters, like that. But, what if you have large number of parameters it will be so tough to create constructor telescopes. That's why we are using builder patterns.
Builder Pattern vs Setters: (My understanding from googling. Feel free to correct if you find it wrong) There are classes without setters, called "Immutable Classes". They are used in distributed and multi-threaded programs where multiple threads can alter the states of an object. Developers use 'synchronize' to prevent this. But a more convenient approach is to use Immutable classes so that threads can't alter the states via setters. (If states need to be altered a new object is created! This consumes memory which is a cons of Immutable Classes but its worth, compared to the state-altered-havocs) Thus for Immutable Classes, Builder Pattern has to be used instead of setters.
Thanks for your response. But I can achieve the same effect by using an empty constructor(Phone phone = new Phone();). Then I can use "setters" to set the required variables, right?
One difference by using builder partner over setters is that Builder pattern creates immutable classes while using setters create mutable classes. But this can not be seen as an advantage in all scenarios.
The problem is that the explanation of the Builder pattern is not correct. The Director is missing. When you have a Director, who calls the Builder then the client becomes decoupled from the Builder. The goal of the Builder pattern is to simplify creating complex objects. When you don't have a Director it's not really that simple for the client since they need to call all of the setter methods, when you introduce the Director that is not the case. You need to look at the GoF explanation of the Builder pattern.
Are we creating 2 objects here for creating just One? 1. When calling new PhoneBuilder() in Main class. 2. When calling new Phone() in PhoneBuilder class.
instead of Phone p = new phonebuilder().setOs("android"); you should write it like this Phone p = new phonebuilder().setOs("android").getPhone(); because "p" is a Phone type variable and you are storing a PhoneBuilder object in it thats why it is showing the error
While learning selenium on some knowledge of java i got stuck at Actions class and then Action interface reference was created and was used to call the multiple functions in single line..my brain was working in right direction but stuck against obstacle where my knowledge ended up..i got some hint on google about fluent interface.. design pattern..and then telusko was the first place to learn..thanks for enlightenment!
1)WHY BUILDER DESIGN? The builder design pattern provides a way for us to build complex immutable objects . Immutable objects are those whose attributes can't be changed (they are 'final') which means there is no setters . 2)WHAT IS THE NEED OF CREATING SEPARATE CLASS ? If you put all the methods of PhoneBuilder class in the phone class,that means you are putting all the setters in the Phone class itself . NOTE: Here the class is not a immutable class anymore,Since we added Setters method in Phone class And also you will end up creating Object for the phone class directly in the main() , try to prevent this kind of behaviour , only let a mutable class(PhoneBuilder in this case ) create and return the object of Phone .
@@saravananm2280 I think without creating a separate class and by using builder class as Innerclass of object and passing builder as input to the object constructor and creating phone object in getPhone method will solve I think, In this way we can avoid creating separate class
aap bhagwan se kam nai ho sir mere lie programming me interested banane se leke aaj tak itni sari chize kyaaa khubsurat tareeke se samjai hai❤❤❤❤ Real superhero, bringing great values in our lives❤️🌼🌼🙏💐
Why u r returning this from each setter..? we can create PhoneBuilder builder = new PhoneBuilder(); and use builder.setXXX(); at the end we will be having phone. this is can be done just using simple setters right? how ur example benefits?
@Telusko you should have explained the advantage of Builder Pattern over setters with no-arg constructor. What's the point in such a video if all the viewers are getting confused after watching it? Have been enjoying your videos fora long time, but not anymore. This is how a product/company loose customers. You could have at least gone through the comments.
Hello Telusko, Since Phone p=new PhoneBuilder().setOS............... at @7:36 2 questions 1. reference var p is going to point 2 objects in heap a new PhoneBuilder() object and new Phone() object? 2. Can we define Phone class instead as an interface and then have PhoneBuilder implements it? That way, no one can create a new instance of an interface? But that wouldn't make sense since we actually need to create Phone() instance but want it to be created by builder class right? So how can we avoid someone creating direct instance of Phone class?
Really don't get this why we do so...Isn't it the same as we initial object with default constructor and just use setters to set values for some attributes? Where is benefits for creating Builder class?
@Telusko you should have explained the advantage of Builder Pattern over setters with no-arg constructor. What's the point in such a video if all the viewers are getting confused after watching it? Have been enjoying your videos fora long time, but not anymore. This is how a product/company loose customers. You could have at least gone through the comments.
What if we need to create a immutable object using builder pattern? Because in this example one of the set method can be called after the object creation and hence can change the state of the object. something like this p.setProcessor() ;
@@GauravSingh-ov9mh I believe the state of the object wont be changed after object creation. If we call the any of the set method on phonebuilder object, only that phonebuilder object's value will be changed, not of the phone object. Do try to run this query.
Hi Telusko, Your explanation is really clear with understandable examples. Thank you. I have an interview soon, I wanted to know which design patterns are most important to know for my interview?