1 00:00:00,880 --> 00:00:06,820 The first thing that we are going to discuss about is the builder factor, so the builder pattern is 2 00:00:06,820 --> 00:00:13,750 a technique that is widely used by software engineers when trying to manage the creation of complex 3 00:00:13,750 --> 00:00:14,290 objects. 4 00:00:14,680 --> 00:00:17,800 But what exactly do we need to use the builder, Patrick? 5 00:00:19,010 --> 00:00:27,020 Well, our situation would be when trying to create complex objects, that requires a lot of parameters, 6 00:00:27,020 --> 00:00:31,520 especially when those parameters need to be supplied at any summarisation phase. 7 00:00:31,560 --> 00:00:38,360 So in a constructor, for example, or when trying to prevent mistakes from creating a complex object 8 00:00:38,720 --> 00:00:46,600 by supplying the parameters that we need to supply in a different order than the one that is expected. 9 00:00:47,360 --> 00:00:59,090 And also when trying to avoid unreadable code that is caused by big constructors, when those big constructors 10 00:00:59,090 --> 00:01:09,580 are needed in order to create some really, really complicated objects in our program, let's see a 11 00:01:09,650 --> 00:01:13,970 typical issue that can be solved by using the builder pattern in the code. 12 00:01:15,680 --> 00:01:22,160 So let's assume that we are working on an online store application, so the most complex object that 13 00:01:22,160 --> 00:01:26,120 I can think of is the user account for our online store. 14 00:01:26,480 --> 00:01:31,400 So like most of those stores, we are collecting some data regarding the user. 15 00:01:31,400 --> 00:01:34,070 So we are assigning an ID for it. 16 00:01:35,630 --> 00:01:42,380 We also collect the names you address, the budget and the discount rate that we may apply to to this 17 00:01:42,380 --> 00:01:42,770 user. 18 00:01:43,250 --> 00:01:51,470 And this is, of course, another simplified example, because applications tend to be more complex 19 00:01:51,740 --> 00:01:55,850 than to have more complex objects inside them. 20 00:01:56,210 --> 00:02:00,560 So now you can see this constructor here. 21 00:02:00,560 --> 00:02:04,490 It's it doesn't look good at all. 22 00:02:04,490 --> 00:02:08,900 It has a long list of parameters that we need to supply in it. 23 00:02:08,930 --> 00:02:11,840 So this is considered to be a bad practice, right. 24 00:02:11,840 --> 00:02:19,010 Because you can easily mistake the order of the parameters so you can easily put inside the budget. 25 00:02:19,010 --> 00:02:23,830 You can put other parameters like we will see in in the example. 26 00:02:24,140 --> 00:02:29,630 So you should avoid this type of these types of constructors right. 27 00:02:29,630 --> 00:02:30,460 In your code. 28 00:02:31,160 --> 00:02:32,730 But you can see it's pretty straightforward. 29 00:02:32,750 --> 00:02:40,580 So we are assigning the ID, the name, the address budget in this country to each and every property 30 00:02:40,580 --> 00:02:41,690 of this class. 31 00:02:42,120 --> 00:02:52,220 So let's see in the upper class here where we have the main method, we created two objects, two users. 32 00:02:52,460 --> 00:02:53,590 The first one is John Smith. 33 00:02:53,960 --> 00:03:00,890 And here we are saying I need a name and address, a budget and also a discount rate. 34 00:03:00,950 --> 00:03:09,350 So basically, this user, we have a discount of two percent to our products. 35 00:03:09,710 --> 00:03:17,210 And for the second one, you can see that it's pretty much the same except for the data itself. 36 00:03:17,510 --> 00:03:22,650 But here we have the budget of two and a discount rate of 100 percent. 37 00:03:23,180 --> 00:03:33,320 So basically we reduced the budget of the user and also we applied a discount of 100 percent to everything 38 00:03:33,320 --> 00:03:35,570 that this user will be buying. 39 00:03:35,870 --> 00:03:45,120 So this this is actually an example of mistakingly putting the wrong parameters in the wrong border. 40 00:03:45,740 --> 00:03:49,540 So we need to find a way to prevent this type of mistake. 41 00:03:49,550 --> 00:03:55,460 And by the way, if you know such such an application, such an online store, just let me know, because 42 00:03:56,000 --> 00:04:02,800 this this can be a very useful issue to exploit. 43 00:04:02,930 --> 00:04:03,310 Right. 44 00:04:03,830 --> 00:04:13,670 So joining me in the next video where we are going to talk about how exactly we can prevent such such 45 00:04:13,670 --> 00:04:14,090 an error.