So the crux of the differentiation at last is that builder design pattern doesn't support dynamic creation of objects whereas decorator design pattern support dynamic creation of objects. Thank you for amazing explanation sir ❤
Hi, it seems adding Director is like having extra implementation, String Builder doesn't have it, but it is good to have it, if you need direct access to object with specific list of parameters.
Hi Shreyansh, great video. I have a question : I understand the purpose of builder is to put the code required to construct an object in each step, so it can be reusable while building various representations of complex objects while also dealing with constructor parameter explosion problem. But I still don't get why another class(builder) is needed. Say in above example, can't we have the set methods in Student class itself? The student will have constructor with no arguments where initially all the fields will be set to null and then the director can interact directly call methods of Student class in sequence instead of Student builder class. This deals with constructor parameter explosion problem .This also reduces code size and duplication of fields in builder class.
i have the same question too , why to have a seprate builder with same attribute the set them then pass it to product class and then get the product build we can have setter in product class with default constructor and setters for each attribute.
we cant have the setter methods within the student class to address the immutability problem. In many situations, we want the object created to be immutable, if you have the setter methods within the student class, then its objects would be mutable. If you use the builder class, then we can selectively set the values and return the final object only after constructing the entire object (with the values that are provided for mandatory attributes and default values for the optional attributes)
Hi @Shreyansh I am completely confused with the explanation for Builder and Decorator pattern difference. Why Builder will create different class. Base pizza, cheese, Mushroom... all these will be just variable of Pizza Builder if we want base +cheese then we will call pizzaBuilder.set(base). set(cheese). If we want base+mushroom then. pizzaBuilder.set(base). set(mushroom). If we want base +cheese. + mushroom then we will call pizzaBuilder.set(base). set(cheese). set(mushroom). Please clarify Thanks!!
understand the usage of builder pattern: consider this scenario: - you want to create an immutable object (and generally immutable object when created its initialized via constructor and no setter method exist in the class). without builder pattern, you have to write so many constructor which will cause an issue. But With builder pattern, we can easily achieve this by exposing the setter methods in Builder class so that we can achieve step by step construction and at end called the build method, which will call the constructor and pass the builder object. So i would say, when you have to achieve the immutability, then it will help and also increase the readibility. hope this clarifies.
@@ConceptandCoding Hi Shreyansh, @stuti5700 was asking about why did we choose decorator pattern to create Pizzas where as it can be created using the builder pattern only.
@@MOHDSALMAN-sj2zu I got your doubt, Let me clarify and if i am wrong please correct me. Using builder pattern you are creating pizza like basePizza + Cheese + Mushroom, but inorder to create such combination of pizza you need to make sure that all these elements are already there in Builder. Now, dynamically you want to add some more toppings which are not in builder then with 2nd principle of SOLID(Open for extension but closed for modification), you can not add more topping to already running(or live) class. But with Decorator patterns, each new topping is a new class which is not violating any principle and that's why for pizza topping example, we use decorator. What i understood: Builder pattern is only for creating a single object but with different kind (or permutation) of properties should be initialized, with creation of object(i.e. permutaions of properties no. of constructor required) then builder pattern is used because in reality we may not use all kind of constructor and we don't know which kind of constructor we need so, we are forced to create and that issue solved by builder pattern. Decorator pattern is used when structural objects(as said in last part of video) or combination of different class kind of object required then it is used.
I have one doubt, Why can't we use the dummy class itself to make a builder, public class Student{ int rollno string firstName string lastName Student(int rollNo){ this.rollNo = rollNo } Student withFirstName(String firstName){ this.firstName = firstName; return this; } } making student like below, Student student = new Student(1).withFirstName("abcd").withLastName("pqrs");
assume a class with 30-40 fields. Having a constructor this big is in itself not practical. Moreover everytime you create an object, of the class, you have to pass all 30-40 arguments into the constructor, irrespective of them being null or otherwise.
Hi SHREYANSH, rather than checking instance of builder object in Director.method. We can also Extend directors to MbaDirector, BcaDirector. What are your thoughts on this one. is that because in future we are not planning to add additional functionality to directors based on builder...and we only need director to initialize builder, so we are using on 1 Director, am I right?
Hi Shreyansh, Thanks for such a great content. Someone asked me in an interview Why to use Builder Pattern, I can create an object by Default Constructor and then I can call corresponding setter methods. Can you please put light on concept behind this. Once Again Thanks to you for such a wonderful content!!!
Hi, as far as i remember, i have explained this on video buddy, let me recollect, the problem is when we have to create object with different different parameters, and having so many constructor is not right. That's what builder pattern solves the issue.
@@ConceptandCoding that's fine, but here the question is am creating an object with default constructor then using setters I am setting the values. In builder also we have to select the corresponding field methods same way we are using setters. like Student student= new Student(); student.setName() student.setAge() whatever data fields you need you can specify setters
Okay, consider this scenario: - you want to create an immutable object (and generally immutable object when created its initialized via constructor and no setter method exist in the class). without builder pattern, you have to write so many constructor which will cause an issue. But With builder pattern, we can easily achieve this by exposing the setter methods in Builder class so that we can achieve step by step construction and at end called the build method, will will call the constructor and pass the builder object. So i would say, when you have to achieve the immutability, then it will help and also increase the readibility. hope this clarifies.
@@chiveshupneja1593 + It is not usually a good practice to return the object with not everything initialized the way you need it to be. If you are just doing it for the small project it makes sense, but think about a bigger system where only you are not in control of creating and calling set methods. In many cases set methods call can be missed and lead to bugs. This pattern ensures everything gets done neatly.
No doubt about content of this channel. It's super cool . But I will be honest on this video that I got confused. 1. Why student director is added ? If it is not important, doesn't it mean this example is not a good fit for builder ? 2. Currently director just sets random value to parameters (member variable) and if it has to be user input how to do this. Mostly this all are not hardcode values but user input. If those are user input and need to set it to class meber, what is fun of passing it first to student builder and then student? If problem is not observed by passing user input to strudent builder class, why not to pass to student class directly.
Hi Ketan, sorry to hear that you got confused with explanation. Let me try to elaborate: - Director work to make Client less bulky, so certain business logic of creating objects like multiple methods invocation (as i mentioned it's a step by step creation of object) can be kept at Director. And client just simply say, createObject. - Directly working on Student object, will make things messy, if there are less say 20 types of students you have to create (engineer, MBA, school, CA, CS students etc) And each of these 20 types of students have certain unique implementation in their method like subjects, admission process etc. Then who will keep those logic. Builder is the right place for that. But let me know, if Live LLD session is Rey, this weekend i will keep one.
@@ConceptandCoding thanks for response. I am sorry but I am yet not able to understand how builder in student class helps here. If client need to pass user input to the builder , let it directly passed to the student. There might be problem and benifit and i am not able to capture it. Would you please share (if possible ) a source code link which takes care of student data as user input and builder helps to ease it? May be that will make it clear for me. Right now i am having feeling that client sets user input to builder which can directly be set to student as both class have same data members.
@@ConceptandCoding The director class in this method seems pure hardcoding, and the reasoning of "Director work to make Client less bulky" seems like an explaination of factory class
I just now checked, it's working, check the description, i have provided the link there gitlab.com/shrayansh8/interviewcodingpractise/-/tree/main/src/LowLevelDesign/DesignPatterns/BuilderDesignPattern
Hi Shrayansh We can also add property step by step of Student in student class only by making setter methods for each property....so why did we make different class like StudentBuilder?
Why just we don't create setter methods in Student Class itself for all the optional attributes and create a constructor with all mandatory attributes. If we do that then we won't need a separate builder class. What do you think?
Okay, consider this scenario: - you want to create an immutable object (and generally immutable object when created its initialized via constructor and no setter method exist in the class). without builder pattern, you have to write so many constructor which will cause an issue. But With builder pattern, we can easily achieve this by exposing the setter methods in Builder class so that we can achieve step by step construction and at end called the build method, will will call the constructor and pass the builder object. So i would say, when you have to achieve the immutability, then it will help and also increase the readibility. hope this clarifies.
Hi Harshit, if you add Student inside Student builder, then getter and setter would be getStudent and setStudent. Or You will have to write all setters and getters by looking at Student class and expose it in builder. What if new field introduced in Student, then whether you have to write it's getter or setter method in builder class or not. If builder class has those fiels, i think it become easy, all the fields which are in builder class we can create getter and setter for them easily. That's my take.
i think you mixed the API optional parameter with method parameter. in java also for api optional parameter, its not mandatory to pass. but pls let me know, if is misunderstood your comment.
@@ConceptandCoding I am taking about optional parameter like int? number which is not supported in java method. In java it is mandatory to pass arguments if defined in method or do method overloading
LLD Basics to Advanced Playlist: th-cam.com/play/PL6W8uoQQ2c61X_9e6Net0WdYZidm7zooW.html
HLD Basics to Advanced Playlist: th-cam.com/play/PL6W8uoQQ2c63W58rpNFDwdrBnq5G3EfT7.html
1:1 on topmate: topmate.io/concept_coding
So the crux of the differentiation at last is that builder design pattern doesn't support dynamic creation of objects whereas decorator design pattern support dynamic creation of objects.
Thank you for amazing explanation sir ❤
Thanks is a very small word for this level of quality content for free.❤️❤️
Thanks Lucifer
Hare Krishna Bhaiya..! Great Video🙂
You are really Talented, Kind & generous.
This is quite helpful video. I saw a couple of other video but this explains the design pattern quite well.
Wow...such real life example...made it easy to understand..thank you soo much
Thanks
Hi Shreyansh, great explanation and thank you so much for the content and the excellent road map that you have.
Thanks
Hi, it seems adding Director is like having extra implementation, String Builder doesn't have it, but it is good to have it, if you need direct access to object with specific list of parameters.
Good night? I'm watching this in the morning (as I'm unemployed lol)
Awesome... The differentiation at last was long due.
Thanks
Hey please add mock interviews from SDE1 expectations perspective. Btw thank you for video. Design pattern videos by you are the best in the world.
Noted thank you
Amazing explanation
Thank you
Hi Shreyansh, great video. I have a question :
I understand the purpose of builder is to put the code required to construct an object in each step, so it can be reusable while building various representations of complex objects while also dealing with constructor parameter explosion problem. But I still don't get why another class(builder) is needed. Say in above example, can't we have the set methods in Student class itself? The student will have constructor with no arguments where initially all the fields will be set to null and then the director can interact directly call methods of Student class in sequence instead of Student builder class. This deals with constructor parameter explosion problem .This also reduces code size and duplication of fields in builder class.
same question
i have the same question too , why to have a seprate builder with same attribute the set them then pass it to product class and then get the product build we can have setter in product class with default constructor and setters for each attribute.
we cant have the setter methods within the student class to address the immutability problem. In many situations, we want the object created to be immutable, if you have the setter methods within the student class, then its objects would be mutable.
If you use the builder class, then we can selectively set the values and return the final object only after constructing the entire object (with the values that are provided for mandatory attributes and default values for the optional attributes)
@@sanjaykatta6499 Thanks for this clear explanation. Could directly relate it to String and StringBuilder
@@sanjaykatta6499Thanks for the explanation, I could only think of your 2nd point but 1st point is also important
Hi @Shreyansh I am completely confused with the explanation for Builder and Decorator pattern difference. Why Builder will create different class. Base pizza, cheese, Mushroom... all these will be just variable of Pizza Builder if we want base +cheese then we will call pizzaBuilder.set(base). set(cheese). If we want base+mushroom then. pizzaBuilder.set(base). set(mushroom). If we want base +cheese. + mushroom then we will call pizzaBuilder.set(base). set(cheese). set(mushroom). Please clarify Thanks!!
Ack, I will clarify you (I will post my comment tomm)
@@ConceptandCoding please clarify
understand the usage of builder pattern:
consider this scenario:
- you want to create an immutable object (and generally immutable object when created its initialized via constructor and no setter method exist in the class). without builder pattern, you have to write so many constructor which will cause an issue.
But With builder pattern, we can easily achieve this by exposing the setter methods in Builder class so that we can achieve step by step construction and at end called the build method, which will call the constructor and pass the builder object.
So i would say, when you have to achieve the immutability, then it will help and also increase the readibility.
hope this clarifies.
@@ConceptandCoding Hi Shreyansh, @stuti5700 was asking about why did we choose decorator pattern to create Pizzas where as it can be created using the builder pattern only.
@@MOHDSALMAN-sj2zu I got your doubt, Let me clarify and if i am wrong please correct me.
Using builder pattern you are creating pizza like basePizza + Cheese + Mushroom, but inorder to create such combination of pizza you need to make sure that all these elements are already there in Builder. Now, dynamically you want to add some more toppings which are not in builder then with 2nd principle of SOLID(Open for extension but closed for modification), you can not add more topping to already running(or live) class.
But with Decorator patterns, each new topping is a new class which is not violating any principle and that's why for pizza topping example, we use decorator.
What i understood:
Builder pattern is only for creating a single object but with different kind (or permutation) of properties should be initialized, with creation of object(i.e. permutaions of properties no. of constructor required) then builder pattern is used because in reality we may not use all kind of constructor and we don't know which kind of constructor we need so, we are forced to create and that issue solved by builder pattern.
Decorator pattern is used when structural objects(as said in last part of video) or combination of different class kind of object required then it is used.
I have one doubt, Why can't we use the dummy class itself to make a builder,
public class Student{
int rollno
string firstName
string lastName
Student(int rollNo){
this.rollNo = rollNo
}
Student withFirstName(String firstName){
this.firstName = firstName;
return this;
}
}
making student like below,
Student student = new Student(1).withFirstName("abcd").withLastName("pqrs");
Most important one 🕜🕜
Thanks for uploading this one
Thanks Rahul
Why can't we have a constructor with all fields, but pass only required values and remaining just null?
assume a class with 30-40 fields. Having a constructor this big is in itself not practical. Moreover everytime you create an object, of the class, you have to pass all 30-40 arguments into the constructor, irrespective of them being null or otherwise.
sir you teach well. but please take one example at a time. that will help you and the audience to learn the topic and apply it twice one after another
Noted.
Hi SHREYANSH, rather than checking instance of builder object in Director.method. We can also Extend directors to MbaDirector, BcaDirector. What are your thoughts on this one.
is that because in future we are not planning to add additional functionality to directors based on builder...and we only need director to initialize builder, so we are using on 1 Director, am I right?
isn't @builder that we use on models a shorter way to implement builder pattern ??
Bhayiya please ek video concurrency ke upar bhi. How to tackle concurrency problems.
Sure
Hi Shreyansh! Could you explain how will client pass student details to director?
Hi Shreyansh, Thanks for such a great content.
Someone asked me in an interview Why to use Builder Pattern, I can create an object by Default Constructor and then I can call corresponding setter methods. Can you please put light on concept behind this.
Once Again Thanks to you for such a wonderful content!!!
Hi, as far as i remember, i have explained this on video buddy, let me recollect, the problem is when we have to create object with different different parameters, and having so many constructor is not right.
That's what builder pattern solves the issue.
@@ConceptandCoding that's fine, but here the question is am creating an object with default constructor then using setters I am setting the values. In builder also we have to select the corresponding field methods same way we are using setters.
like Student student= new Student();
student.setName()
student.setAge()
whatever data fields you need you can specify setters
Okay, consider this scenario:
- you want to create an immutable object (and generally immutable object when created its initialized via constructor and no setter method exist in the class). without builder pattern, you have to write so many constructor which will cause an issue.
But With builder pattern, we can easily achieve this by exposing the setter methods in Builder class so that we can achieve step by step construction and at end called the build method, will will call the constructor and pass the builder object.
So i would say, when you have to achieve the immutability, then it will help and also increase the readibility.
hope this clarifies.
@@ConceptandCoding yes, got it.
Thankyou!!
@@chiveshupneja1593 + It is not usually a good practice to return the object with not everything initialized the way you need it to be. If you are just doing it for the small project it makes sense, but think about a bigger system where only you are not in control of creating and calling set methods.
In many cases set methods call can be missed and lead to bugs.
This pattern ensures everything gets done neatly.
No doubt about content of this channel. It's super cool . But I will be honest on this video that I got confused.
1. Why student director is added ? If it is not important, doesn't it mean this example is not a good fit for builder ?
2. Currently director just sets random value to parameters (member variable) and if it has to be user input how to do this. Mostly this all are not hardcode values but user input. If those are user input and need to set it to class meber, what is fun of passing it first to student builder and then student? If problem is not observed by passing user input to strudent builder class, why not to pass to student class directly.
Hi Ketan, sorry to hear that you got confused with explanation.
Let me try to elaborate:
- Director work to make Client less bulky, so certain business logic of creating objects like multiple methods invocation (as i mentioned it's a step by step creation of object) can be kept at Director.
And client just simply say, createObject.
- Directly working on Student object, will make things messy, if there are less say 20 types of students you have to create (engineer, MBA, school, CA, CS students etc)
And each of these 20 types of students have certain unique implementation in their method like subjects, admission process etc. Then who will keep those logic. Builder is the right place for that.
But let me know, if Live LLD session is Rey, this weekend i will keep one.
@@ConceptandCoding thanks for response.
I am sorry but I am yet not able to understand how builder in student class helps here. If client need to pass user input to the builder , let it directly passed to the student. There might be problem and benifit and i am not able to capture it.
Would you please share (if possible ) a source code link which takes care of student data as user input and builder helps to ease it? May be that will make it clear for me. Right now i am having feeling that client sets user input to builder which can directly be set to student as both class have same data members.
@@ConceptandCoding The director class in this method seems pure hardcoding, and the reasoning of "Director work to make Client less bulky" seems like an explaination of factory class
gitlab link is not working. Please give the latest link
I just now checked, it's working, check the description, i have provided the link there
gitlab.com/shrayansh8/interviewcodingpractise/-/tree/main/src/LowLevelDesign/DesignPatterns/BuilderDesignPattern
@@ConceptandCoding Can you please explain how will this solve the problem if we want to take user input?
Hi Shrayansh
We can also add property step by step of Student in student class only by making setter methods for each property....so why did we make different class like StudentBuilder?
yes you can, but main advantages of builder pattern are:
- readability and maintainability
- Creation of immutable objects
oh, thanks@@ConceptandCoding
isn't factory design pattern also used here ?
Why just we don't create setter methods in Student Class itself for all the optional attributes and create a constructor with all mandatory attributes. If we do that then we won't need a separate builder class. What do you think?
Okay, consider this scenario:
- you want to create an immutable object (and generally immutable object when created its initialized via constructor and no setter method exist in the class). without builder pattern, you have to write so many constructor which will cause an issue.
But With builder pattern, we can easily achieve this by exposing the setter methods in Builder class so that we can achieve step by step construction and at end called the build method, will will call the constructor and pass the builder object.
So i would say, when you have to achieve the immutability, then it will help and also increase the readibility.
hope this clarifies.
Could you please make videos for designing DB also for multiple cases. Thanks in advance.
noted
cant see gitlab link, i am a member too
Why Can't we pay using UPI. Is card mandatory for joining.
It's TH-cam which shows different payment methods. I have no control over it Mayuri.
Hi @Concept&&Coding , these videos are good but can you please share any books or other resources which might help in addition to these videos.
For design patterns, you can follow "head first design pattern" else there is no book for LLD, its just an experience learning
Hi sir, Thank You For video. I have one doubt, can't we have Student object in StudentBuilder it self? It'll remove duplication right?
Hi Harshit, if you add Student inside Student builder, then getter and setter would be getStudent and setStudent.
Or
You will have to write all setters and getters by looking at Student class and expose it in builder. What if new field introduced in Student, then whether you have to write it's getter or setter method in builder class or not.
If builder class has those fiels, i think it become easy, all the fields which are in builder class we can create getter and setter for them easily.
That's my take.
@@ConceptandCoding ok sir got it thank yoy
One note link ? Good video on builder design pattern
Thanks will share
Hi can you cover ddd and clean architecture
Sure
Thats why we call JAVA a fucked up language in other language optional fields not need to be passed
i think you mixed the API optional parameter with method parameter.
in java also for api optional parameter, its not mandatory to pass.
but pls let me know, if is misunderstood your comment.
@@ConceptandCoding I am taking about optional parameter like int? number which is not supported in java method. In java it is mandatory to pass arguments if defined in method or do method overloading
ur awesome bro... ily
Thank you ❤️