Builder Design Pattern in Java

  Рет қаралды 231,021

Telusko

Telusko

Күн бұрын

Пікірлер: 120
@SushilKumar-wt7js
@SushilKumar-wt7js 4 жыл бұрын
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
@vyshnavramesh9305
@vyshnavramesh9305 5 жыл бұрын
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.
@SagarSagar-ro3fj
@SagarSagar-ro3fj 6 жыл бұрын
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!
@charlessnachez4248
@charlessnachez4248 2 жыл бұрын
Thank you man, you got a new subscriber, my teacher couldn't explain this to us, he didn't even mention that this is design pattern.
@Venkat.devisetti
@Venkat.devisetti 6 жыл бұрын
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
@shaliniagarwal8925
@shaliniagarwal8925 Жыл бұрын
Excellent explanation. I was about to give up on this topic. You explained so clearly - made it so easy !!
@smitj5404
@smitj5404 3 ай бұрын
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❤️🌼🌼🙏💐
@codespector1989
@codespector1989 2 жыл бұрын
finally someone who explained in very simple word , thanks
@mayanksrkumar16
@mayanksrkumar16 Жыл бұрын
Amazing! Explained such a complex topic in such a simple way! Thanks a lot!
@thestarinthesky_
@thestarinthesky_ 4 жыл бұрын
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.
@BadCode_22
@BadCode_22 3 жыл бұрын
Agree with this comment
@anjalat3016
@anjalat3016 4 жыл бұрын
Your explanation is very easy to understand Navin. Thanks a lot.Could you please do few videos on other Design Patterns as well.
@surya5064
@surya5064 3 жыл бұрын
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.
@Fer-jf3pl
@Fer-jf3pl 5 жыл бұрын
Wow thank you so much. Ur tutorials are always the best
@savelokhosa6846
@savelokhosa6846 2 жыл бұрын
You are one of the best Navin
@tirupatirao7521
@tirupatirao7521 4 жыл бұрын
your analogy or examples are simply and good to understand
@lux27.42
@lux27.42 5 жыл бұрын
da*n, this tutorial seriously easy to understand!! thankyou Mr.Alien..!!
@saurabhshukla4900
@saurabhshukla4900 4 жыл бұрын
Nice and clear example. But builder need not be instantitated everytime. So we can make the builder static.
@Sumitmaurya070
@Sumitmaurya070 3 жыл бұрын
wow sir, i couldnt answer it in mnc interview, now i will not do mistake again due to this vdo
@ymfeijo
@ymfeijo 4 жыл бұрын
Very clear and tangible explanation!
@michelegenchi
@michelegenchi 8 жыл бұрын
Very easy example; now I understand , thanks... I share your video in my linkedln
@Corporate_Kida
@Corporate_Kida Жыл бұрын
Thankyou so much for making it so simple.👍
@Madhavan2020
@Madhavan2020 8 жыл бұрын
Thanks dude, its awesome simplest example to remember.
@milad_mo
@milad_mo 2 жыл бұрын
Thanks for very useful and understandable explanation
@dinushachathuranga7657
@dinushachathuranga7657 Жыл бұрын
Thanks for excellent explanation❤
@suchitrasrinivas4330
@suchitrasrinivas4330 Жыл бұрын
Thank you sir, Very nice explanation.,
@unemployedcse3514
@unemployedcse3514 8 ай бұрын
awesome , luv u brother ❤
@EchoVids2u
@EchoVids2u 4 жыл бұрын
thanks bro, your tutorials are always so clear and concise
@palashkhatri7820
@palashkhatri7820 7 жыл бұрын
Very Nicely Explained , Got the Concept , Thank You
@ReihaneZekri
@ReihaneZekri 7 жыл бұрын
Great explanation! just one question: why do we need a separate class for Builder pattern? isn't it ok to use the same entity class?
@saravananm2280
@saravananm2280 5 жыл бұрын
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 .
@chaitanyajada5430
@chaitanyajada5430 2 жыл бұрын
@@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
@ehsanhosseini5861
@ehsanhosseini5861 4 жыл бұрын
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?
@pukka4140
@pukka4140 4 жыл бұрын
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?
@sachindixit6605
@sachindixit6605 7 жыл бұрын
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.
@saurabhkumar-kh7oz
@saurabhkumar-kh7oz 6 жыл бұрын
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.
@ayushshukla1597
@ayushshukla1597 3 жыл бұрын
Amazing explanation. Thank you.
@saddamjamali9273
@saddamjamali9273 5 жыл бұрын
Thanks for such clear explanation.
@niharikakumar1533
@niharikakumar1533 Жыл бұрын
Great sir 😊
@VipinRawat_Offcial
@VipinRawat_Offcial 2 жыл бұрын
You made design patterns so easy
@rajivraghunathan9104
@rajivraghunathan9104 8 жыл бұрын
awesome! simplest ever explanation
@kbhardwaj1989
@kbhardwaj1989 6 жыл бұрын
very beautifully explained. Thanks for sharing knowledge.
@leetcodebaby6680
@leetcodebaby6680 6 жыл бұрын
Loved it, truly amazing ! Thanks !
@vishaldindalkop294
@vishaldindalkop294 3 ай бұрын
Thank you for the video
@viswanathnuggu
@viswanathnuggu 8 жыл бұрын
Thanks, it is helpful! So, here in this example Phone class will have only getters and no setters?
@vasudevparmar8812
@vasudevparmar8812 2 жыл бұрын
using Interface this could be done:- public interface Phone { Phone setOs(String os); Phone setBattery(int val); Phone SetScreensize(int size); Phone setProcessor(String processor); Phone setRam(int ram); Phone getPhone(); } public class PhoneBuilder implements Phone{ private String os; private int Battery; private int size; private String processor; private int ram; @Override public Phone setOs(String os) { this.os=os; return this; } @Override public Phone setBattery(int val) { Battery=val; return this; } @Override public Phone SetScreensize(int size) { this.size=size; return this; } @Override public Phone setProcessor(String processor) { this.processor=processor; return this; } @Override public Phone setRam(int ram) { this.ram=ram; return this; } public Phone getPhone() { return this; } public String toString(){ return "Mobile details [ os: "+os+", battery: "+Battery+", screensize: "+size+", processor: "+processor+", ram: "+ram+" ]"; } } public class client { public static void main(String[] args) { Phone p1=new PhoneBuilder().setBattery(3000).setOs("Android").setRam(4).getPhone(); System.out.println(p1); Phone p2=new PhoneBuilder().SetScreensize(12).setBattery(3000).setOs("ios").setRam(8).getPhone(); System.out.println(p2); } }
@munawarabbas1636
@munawarabbas1636 8 жыл бұрын
great effort Bro..... Please CarryON
@nissskygtr
@nissskygtr 6 жыл бұрын
Excellent Explanation !!!! +1
@gunasekar925
@gunasekar925 2 жыл бұрын
awesome sir
@ashishstunner
@ashishstunner 4 жыл бұрын
simple but effective.
@raghavilatha
@raghavilatha 4 жыл бұрын
Thankyou for your explanation, can you please add remaining design patterns also ?
@ThaEquals
@ThaEquals 8 жыл бұрын
Thank you for this! Very helpful!
@parthpatel3639
@parthpatel3639 7 жыл бұрын
well Explained....Thank You
@tarunseetha
@tarunseetha 6 жыл бұрын
Many thanks sir for sharing this
@SushankSaini-g5z
@SushankSaini-g5z Жыл бұрын
Thank you.
@anasabdeljalil9883
@anasabdeljalil9883 3 жыл бұрын
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 .
@SantoshKumar-wh7gb
@SantoshKumar-wh7gb 8 жыл бұрын
awesome example
@JavaAll754
@JavaAll754 4 жыл бұрын
Is this another way of implementing constructor overloading?
@enesog
@enesog 8 жыл бұрын
You can use mulltiple conctructors.This will do the same,no ?
@manindersingh6371
@manindersingh6371 3 жыл бұрын
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?
@thecodeagency2307
@thecodeagency2307 3 жыл бұрын
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.
@manindersingh6371
@manindersingh6371 3 жыл бұрын
@@thecodeagency2307 Thank you :)
@srishamarathe2595
@srishamarathe2595 4 жыл бұрын
I don't understand the dislikes LOL, thanx for the video sir, deeply appreciate it.
@travel_bloggerr
@travel_bloggerr Жыл бұрын
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?
@gigajoules6636
@gigajoules6636 4 жыл бұрын
Must have missed something here. I assume to set default values we just state them where we declare the variables the object is made up of?
@fazaljarral2792
@fazaljarral2792 5 жыл бұрын
What is different from setters? We could do it with default constructor , then set each value? How is this different?
@saurabhshukla4900
@saurabhshukla4900 4 жыл бұрын
Point is you have to make the object immutable. You should not be able to change its value later on. So you cannot expose setters
@bharathys7165
@bharathys7165 8 жыл бұрын
Thanks a ton.. !!!
@sahillamba3879
@sahillamba3879 4 жыл бұрын
How printing the object gave the values of the Phone attributes? It should not right?
@albindavis3538
@albindavis3538 Жыл бұрын
superb
@movieclimax9328
@movieclimax9328 7 жыл бұрын
great job..thanks
@anonymous_devil3730
@anonymous_devil3730 Жыл бұрын
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??
@rahulkaranjavkar7994
@rahulkaranjavkar7994 5 жыл бұрын
Thanks sir...
@200564612434
@200564612434 4 жыл бұрын
@Telusko .... what if i want to pass those values dynamically to the builder class ??? how can i do that ??
@spencerdepas4235
@spencerdepas4235 8 жыл бұрын
Nice video.
@manohargandham1353
@manohargandham1353 7 жыл бұрын
what is the advantage of having a builder over having setters in Phone class ?
@skullwise
@skullwise 7 жыл бұрын
Good question. If you come across the answer to your question, please let me know.
@skullwise
@skullwise 7 жыл бұрын
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?
@Keyakina
@Keyakina 7 жыл бұрын
yeah same thought! thats also exactly what he's doing.. i see no point in using this pattern
@skullwise
@skullwise 7 жыл бұрын
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.
@risteardob2095
@risteardob2095 6 жыл бұрын
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.
@manjunathkotagi4874
@manjunathkotagi4874 2 жыл бұрын
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?
@aishwaryasrivastava6526
@aishwaryasrivastava6526 4 жыл бұрын
please add command design pattern and other design patterns too. thanks
@isoplayers
@isoplayers 6 жыл бұрын
Excellent
@GauravSingh-ov9mh
@GauravSingh-ov9mh 7 жыл бұрын
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() ;
@saurabhkumar-kh7oz
@saurabhkumar-kh7oz 6 жыл бұрын
we can set the parameters in the acutal Phone class as final making it immutable.
@____ayush____
@____ayush____ Жыл бұрын
@@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.
@pravendrayadav6200
@pravendrayadav6200 2 жыл бұрын
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.
@yellowlegend245
@yellowlegend245 6 жыл бұрын
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.
@gdIndia199
@gdIndia199 2 жыл бұрын
Great
@nadiyalokesh7658
@nadiyalokesh7658 2 ай бұрын
Can we have attributes in Phone class?
@brotherandsister9203
@brotherandsister9203 Жыл бұрын
Done...
@mohammedviso2269
@mohammedviso2269 8 жыл бұрын
Thanks
@ritikas383
@ritikas383 5 жыл бұрын
Why to use Super keyword here? when already this will refer to current class's object?
@goatmatata2798
@goatmatata2798 5 жыл бұрын
thanxx
@ronakpurohit750
@ronakpurohit750 Жыл бұрын
what is the the participants in this program and class diagram
@shivakrishna3069
@shivakrishna3069 Жыл бұрын
How toString() method is called ?
@shadab5azhar
@shadab5azhar 5 жыл бұрын
where I will get other videos of design Pattern
@ronyut1984
@ronyut1984 5 жыл бұрын
Why do all the setters return PhoneBuilder and not void?
@lokesh398
@lokesh398 4 жыл бұрын
Can anyone help for me it showing type mismatch when trying to put Phone p = new phonebuilder().setOs("android");
@200564612434
@200564612434 4 жыл бұрын
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
@good114
@good114 24 күн бұрын
🙏♥
@dineshkinibailoor340
@dineshkinibailoor340 7 жыл бұрын
why it is showing processor=null, better approach is to provide standard value using default value constructor
@rasnarahman3058
@rasnarahman3058 7 жыл бұрын
nice
@edgarasstasiukaitis3062
@edgarasstasiukaitis3062 5 жыл бұрын
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?
@vyshnavramesh9305
@vyshnavramesh9305 5 жыл бұрын
@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.
@mdsaquibansari4123
@mdsaquibansari4123 Жыл бұрын
your 8 min video carry more weight than rest present in youtube
@souravkumar1608
@souravkumar1608 11 ай бұрын
Any way, we see here two classes Phone and Phonebuilder containing same entities to avoid redundancy.
@dipak002
@dipak002 5 жыл бұрын
Welcome back "Aliens", did you called us "Aliens"!!?? and why is that 🤔🤔
@venkateshkarunakaran2212
@venkateshkarunakaran2212 5 жыл бұрын
because he doesn't know us , and when someone isn't known to you , you call them alien.
@sarojsahoo8763
@sarojsahoo8763 4 жыл бұрын
This is not how Builder Design Pattern works . It should behave like immutable class . We should not provide setter.
@vishnunairv4u
@vishnunairv4u 4 жыл бұрын
there is no setters in the phone class. The builder is doing the stuff :)
@MrAdy0207
@MrAdy0207 8 жыл бұрын
I would suggest using Intellij
@UmairAli
@UmairAli 4 жыл бұрын
is anyone else getting errors in code or is it just me?
@abhineetsingh6720
@abhineetsingh6720 2 жыл бұрын
👽👽👽👽👽👽👽👽👽👽
@bostonlights2749
@bostonlights2749 4 жыл бұрын
:)
@anitahcu
@anitahcu 3 жыл бұрын
thanks
Adapter Design Pattern in Java Theory
3:55
Telusko
Рет қаралды 184 М.
Prototype Design Pattern in Java
18:30
Telusko
Рет қаралды 148 М.
Ozoda - Alamlar (Official Video 2023)
6:22
Ozoda Official
Рет қаралды 10 МЛН
"Идеальное" преступление
0:39
Кик Брейнс
Рет қаралды 1,4 МЛН
Every team from the Bracket Buster! Who ya got? 😏
0:53
FailArmy Shorts
Рет қаралды 13 МЛН
Builder Design Pattern in Java Theory
4:55
Telusko
Рет қаралды 288 М.
Master the Fluent Builder Design Pattern in C#
15:05
Milan Jovanović
Рет қаралды 29 М.
7 Design Patterns EVERY Developer Should Know
23:09
ForrestKnight
Рет қаралды 145 М.
Adapter Design Pattern in Java
8:21
Telusko
Рет қаралды 170 М.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 317 М.
Builder Design Pattern in Java
12:23
Coding Simplified
Рет қаралды 60 М.