1 00:00:00,900 --> 00:00:07,260 This is an example of the builder pattern diagram, so basically here we have the director class, which 2 00:00:07,260 --> 00:00:15,720 in our example is the object itself, the user accounts class, the user account for the only website, 3 00:00:16,110 --> 00:00:26,480 and it will have a reference to a builder class, which in our example will be coded as a static class. 4 00:00:26,610 --> 00:00:34,620 It can be also in the interface, but we will use it as an inner class and that's builder class will 5 00:00:34,620 --> 00:00:41,810 basically build without using complex constructors will basically build our our object. 6 00:00:41,910 --> 00:00:44,200 So let's dive into the code. 7 00:00:44,220 --> 00:00:44,580 Now. 8 00:00:46,340 --> 00:00:52,010 Let's analyze first the all I saw account object, so basically we have the same properties that we've 9 00:00:52,010 --> 00:01:00,320 seen before, but in addition to this, we have a static inner class like we see here, a builder, 10 00:01:01,880 --> 00:01:07,740 which has also the same the same properties as the the outer class. 11 00:01:07,760 --> 00:01:08,060 Right. 12 00:01:09,230 --> 00:01:11,810 But you will see the difference. 13 00:01:12,080 --> 00:01:16,760 So here we also have a constructor for this builder that accepts Nidhi. 14 00:01:17,150 --> 00:01:23,260 We decided to keep the idea in the constructor because the is pretty important. 15 00:01:23,270 --> 00:01:27,690 We don't want to allow the possibility of creating a user account without an idea. 16 00:01:27,710 --> 00:01:29,090 This may crash the application. 17 00:01:29,100 --> 00:01:30,760 So it's also about practice. 18 00:01:31,010 --> 00:01:33,240 So we will keep this one. 19 00:01:34,280 --> 00:01:41,330 This this property here in the constructor and for the rest of the properties we have Setas, we have 20 00:01:41,330 --> 00:01:48,340 defined centers that will return the object of this this builder. 21 00:01:48,920 --> 00:01:53,620 So we have a center for the name, for the address, for the budget and for the discount. 22 00:01:53,630 --> 00:01:59,460 And you can see that the probability of supplying wrong data is decreasing. 23 00:01:59,480 --> 00:01:59,830 Right? 24 00:01:59,840 --> 00:02:06,350 It's significantly decreased because you can see in the name of the method what type of parameter you 25 00:02:06,350 --> 00:02:13,400 need to supply so that this will lower the chances of making some some mistakes. 26 00:02:14,090 --> 00:02:19,480 And the final method in this builder in our class is the build method. 27 00:02:19,490 --> 00:02:21,080 So this will basically the build. 28 00:02:21,440 --> 00:02:31,850 This will build an online storico object and it will assign to each parameter the corresponding parameter 29 00:02:31,850 --> 00:02:36,840 for from the builder class, and it will also return the object itself. 30 00:02:37,820 --> 00:02:44,660 You can see that I have made the constructor of the online store account class private because we don't 31 00:02:44,660 --> 00:02:51,740 want someone that needs to create an online store account in the code to use the constructor of the 32 00:02:51,740 --> 00:02:52,160 object. 33 00:02:52,370 --> 00:02:59,990 We want to use the builder class instead. 34 00:03:00,020 --> 00:03:00,300 Right. 35 00:03:00,320 --> 00:03:01,790 So with the method. 36 00:03:02,210 --> 00:03:09,800 So also we have getters and setters, but these are not important because the actual getters and setters 37 00:03:09,800 --> 00:03:12,800 are inside the builder class here. 38 00:03:12,980 --> 00:03:20,600 So let's see this in action so we can see now that the the code that resulted from applying this builder 39 00:03:20,600 --> 00:03:24,110 pattern is a lot more stable. 40 00:03:24,110 --> 00:03:28,940 It's more readable, it has better readability. 41 00:03:29,420 --> 00:03:36,580 You can easily understand, even if you're not such a technical person, even if you show this code 42 00:03:36,590 --> 00:03:43,460 to someone that is non-technical, she or he will know what this what this means. 43 00:03:43,460 --> 00:03:43,760 Right. 44 00:03:43,770 --> 00:03:51,290 So we create an online store account with the name John Smith, and here we invoke the builder constructor, 45 00:03:51,500 --> 00:03:53,390 which requires an ID. 46 00:03:53,390 --> 00:04:00,680 So we supply ninety here and then we create we assign a name and address, a budget and also a discount 47 00:04:03,140 --> 00:04:11,960 using this pattern, using the builder class and also at the end we supply to build a method that will 48 00:04:11,960 --> 00:04:14,950 return the object that we created. 49 00:04:14,960 --> 00:04:17,020 The object of this type on my account. 50 00:04:17,390 --> 00:04:21,950 So let's see if this has done what it needed to do. 51 00:04:21,960 --> 00:04:28,790 So basically the system of print can and should output the name of this object and therefore we will 52 00:04:28,790 --> 00:04:32,930 see that the name was correctly stored in the object itself. 53 00:04:33,140 --> 00:04:34,550 So let's validate this. 54 00:04:36,410 --> 00:04:43,880 So you can see that the name is correctly outputted here, so everything went well and there are no 55 00:04:43,880 --> 00:04:44,280 issues. 56 00:04:44,480 --> 00:04:47,210 The properties were set as expected. 57 00:04:48,770 --> 00:04:50,970 That's it for the builder pattern. 58 00:04:51,000 --> 00:04:54,770 Join me in the next story where we are going to discuss about the factory pattern. 59 00:04:55,250 --> 00:04:57,140 So see you in the next tutorial.