 morning welcome back to the channel so today in this video I will be sharing you builder design pattern in Java so taking a real-time example that like in this scenario in my project I have created a builder pattern like based on the builder design pattern I have created an object so I'll be explaining that using an before that whenever like this is one of the important interview question so you can go or you can define the builder design pattern as like this like if you go with a bookish definition it is a separate like builder pattern aims to separate the construction of a complex object from it representation so that the same construction process can create different representations so basically what that means is using multiple blocks with multiple objects we create one super object so using the same different objects we can create one more object as well so that is what it says so and one more definition is builder patterns builds a complex object using a simple objects and using a step-by-step procedure and this comes under creation pattern so that's it about it so let me take the real-time example so I so in my project basically a scenario was there like if you are from a healthcare domain you might be knowing about claims so in the claims processing process now we were asked to create a calculator so this was having some close to like 70 to 75 fields inside it out of which fewer primitive fewer object references and fewer mandatory fewer optional so I am taking a few from that class and I'll be trying to explain that trying to explain the design pattern builder design pattern so let me create few string first claim ID and this was a mandatory well let us call it a required field and one more let me copy it will take a lot of time of private okay let me go with four mandatory for normal fees low cost ID submit date so actually this was a date but I'm going with a string here for better explanation what was submit date and let us go with the amount what was the amount this was long actually but I'm still going so for required fees and let me take for non-required like age was non-required address this was non-required and was a reference object more thing was one was limits if there are any limits on the product object so that was this and one more if I want to go limits okay so I'll go with phone I can go any value which is option so or I can give location from where the claim got submitted okay so this way a few fields so to create a immutable object for this class so we the requirement was to create an immutable class for this object so for that I have to go with the constructor but there was 70 fields and few were optional and few were mandatory so really becomes difficult in so in that situation what will go is we'll go with builder pattern so here we will be only having what I can say just get us not the set us so just rightly we can create I think directly get us and set us for final it won't create select or generate yeah only get us will get created and set us won't get created and let us create two-string method for this so that while we see salt will we get either as we fact source to string use all fields generate so this got generated now if you see it is crying because it has not been initialized so to come to come initialize this we need a builder so let me first create the builder builder class which would be a static class class let me name claims builder class we got so now I have to have again the same fields here so let me copy paste let me just remove this so don't want this as final it is a normal it can be updated okay now we got the fields now if you remember we had four what to say let me create one constructor we were having four mandatory fields till here claim ID custody submitted an amount so we have to create a constructor claim ID string cast ID mid-date let me break this so that isn't single and last thing is string amount so now I'll be using in I'll be initializing so this dot so claim ID equals claim ID so then let me start see this for four things so custody custody submit date as these are mandatory fields I am initializing then through constructor so what happened claim okay and now the other are things I'll be having the methods so here ends the constructor public claims builder age is actually should have created this first only so we should we would have saved some time so you know why we are writing written this you know you comment it out if you don't know this is a homework for you why we have to write that so age is done address limits so basically limits you might get confused limits is nothing but any limitations that the claim has like few are from healthcare domain you it is very easy to understand just that reference objects strike my mind so I have created this so now we have to call the constructor of this class so we have to create private because this is immutable and how to create a constructor claims builder dot builder and we have to initialize the all the variables whatever we have inside it it will take time now claim ID equals builder dot play my ID so let me copy paste these things so that it would be easy for us copy 1 2 3 4 custody custody just submitted amount age I think you guys might be boring because I should have created this earlier and then I should have created the video okay location is there okay yeah location equals builder dot location smart if we see what happened we should be changed we need to how can we have locations so now we require a build method so here comes the magic so let me create a build method public constructor would be the return type and build and inside this build I will create I will call the constructor so how to call return CL that's it so this is how the written spelling like a return CL so this is how we have created a builder class so now what benefit we get out of this I'll explain you so claims calculator now let me test so I have so games calculator game CC equals new games calculator dot no no no I don't think that's calculated dot games calculator of the required fields we need to pass here okay we first build method why it is not coming oh no no games builder claims builder yeah okay and now it will stop crying but the format it is things we need to pass so first one was what was the first one ID okay not showing a chair what is the first one of the clean builder constructor claim ID custody submitted okay so claim ID so CLA we can have iPhone 123 custody we can have sunny 123 submission date 0101 2021 comma a mom book anything 500 done so now I have the object of address so for the first claim claim got submitted from Hyderabad fine and one more object I have is age 25 string 25 so see there were only two we are having only two optional classes so now we are like two optional fields we have we have setting those two optional similarly if I create one more claim CC let us remain CC 1 CC 2 CC 1 so claim 123 789 sunny let's make Johnny also dates can be like anything it doesn't matter so amount we can change to thousand dollars anything so address who name age can be anything then what we have location okay oh address and location both okay location is from where the claim got so claim got submitted from India and what we have limits we can have no limits so see this let us run this and let me print out let us have C source okay so let me directly bring CC 1 CC 2 space let me run this yeah we got two objects now this is the super object and don't think these are there like for an example I have created taken as everything as string but this way in the real time if these were the reference objects so you need to create an unmodified object of that and you need to pass that here as so if you see whatever we have said those values have been set and others have been set as null so you do if at all builder pattern if at all we haven't used builder pattern what we would have done in in this constructor itself sorry in claims calculator constructor itself I would have removed this builder and I would have had all the values and we are making it a mandatory here like if at all we don't have that of value I have to pass the value as null right that could have been done but tomorrow again like the combination of objects will keep increasing as we increase the fields inside this object so now if at all suppose assume there is one more field got a charge which is not phone number so this is now not a mandatory field so now for this this very simple directly you can have one more field here and one getter for the phone so in one class if I make the change that's it but if at all in classes are using this claims calculator and they want to build in all those classes wherever this constructor is used I have to go and check if at all I have the phone number or not now for existing classes I don't need to make any changes I can directly create one more object Johnny Larry 98 some value Delhi we can take no limits now for this object phone is there I'll attach the phone number and if I print this nothing will happen like the third object gets printed let me run this you know the existing objects where is the phone number okay two string method I haven't overridden yeah there are two string man so let me create one new two string method two string generate so let me save this and now let me run this or see the phone number for the existing objects is null and for the new object we have the phone number so as I am creating this in the same class we may make changes here as well but in the real-time scenario it would be a different case you might be using this main complex object somewhere is again you have to make a lot of changes so claims like builder pattern helps us in avoiding like setting the mandatory field whatever fields you have you can set it remaining fields will get in charge normally with null so this is about the builder design pattern in Java with real-time example I hope this might be helpful to at least few and thanks for watching