1 00:00:00,800 --> 00:00:05,110 During the last video, we used a design pattern to build a service registry and 2 00:00:05,110 --> 00:00:06,780 I mentioned that we'd circle back to it. 3 00:00:06,780 --> 00:00:08,660 Well, here we are. 4 00:00:08,660 --> 00:00:14,090 This pattern, called the Builder Pattern, is used to address a few common pit falls. 5 00:00:14,090 --> 00:00:17,540 Mainly, the one of classes that have many fields. 6 00:00:17,540 --> 00:00:21,740 Creating an object is accomplished by calling the classes constructor. 7 00:00:21,740 --> 00:00:25,600 But, if the class has five instance feilds, like ours does. 8 00:00:25,600 --> 00:00:29,070 We might end up with a constructor that has five perameters. 9 00:00:29,070 --> 00:00:31,740 But the code that we type to instantiate an object with 10 00:00:31,740 --> 00:00:34,070 five perimeters isn't that readable. 11 00:00:34,070 --> 00:00:36,240 And what if we want to be able to create an object, 12 00:00:36,240 --> 00:00:39,018 while specifying only a couple field values? 13 00:00:39,018 --> 00:00:42,000 Well, I guess we need a constructor, but 14 00:00:42,000 --> 00:00:44,670 the order of that parameter list may not be so obvious either. 15 00:00:46,010 --> 00:00:48,640 So, we arrive at the builder pattern. 16 00:00:48,640 --> 00:00:49,580 Using this pattern, 17 00:00:49,580 --> 00:00:53,420 we're able to create readable code that's intuitive and easy to use. 18 00:00:53,420 --> 00:00:57,320 To see how this is done, let's do this for our contact class now. 19 00:00:58,970 --> 00:01:01,995 To demonstrate what we'd like to avoid, I'll use similar code here. 20 00:01:01,995 --> 00:01:06,490 It's what we used earlier in the course to construct a sample contact object. 21 00:01:06,490 --> 00:01:09,340 So, I'll create a contact, I'll name it contact. 22 00:01:09,340 --> 00:01:12,170 And I will call a constructor that doesn't really exist but 23 00:01:12,170 --> 00:01:13,730 let's pretend that it does. 24 00:01:13,730 --> 00:01:17,838 I'll pass my first name, last name, email address. 25 00:01:21,038 --> 00:01:22,300 And a phone number 773-555-6666. 26 00:01:22,300 --> 00:01:24,170 Now if we had coded this 27 00:01:28,440 --> 00:01:32,460 constructor in a contact class, as we did in workspaces earlier in the course. 28 00:01:32,460 --> 00:01:35,460 We might remember the order of our four parameters or 29 00:01:35,460 --> 00:01:37,520 we could pop open the source code to see. 30 00:01:37,520 --> 00:01:41,610 Still, this would require an extra step that draws attention to the fact that our 31 00:01:41,610 --> 00:01:43,920 code isn't as readable as it should be. 32 00:01:43,920 --> 00:01:47,670 Even more, what if the contact class came from a jar file we've included in our 33 00:01:47,670 --> 00:01:50,030 project and we don't have access to the source code? 34 00:01:50,030 --> 00:01:51,520 For those reasons and 35 00:01:51,520 --> 00:01:55,550 others, many developers choose the builder pattern to address concerns. 36 00:01:55,550 --> 00:01:57,200 Wouldn't it be much more intuitive and 37 00:01:57,200 --> 00:02:00,390 readable if our code coud look like this instead? 38 00:02:01,500 --> 00:02:03,280 So, I'll create a contact and 39 00:02:03,280 --> 00:02:08,830 builder object to specify something that we feel our users would know. 40 00:02:08,830 --> 00:02:12,400 The first name and the last name and then, I'll put this on a separate line for 41 00:02:12,400 --> 00:02:13,480 readability. 42 00:02:13,480 --> 00:02:18,934 On that object, I could call a withEmail method and specify my email address. 43 00:02:22,268 --> 00:02:26,206 And with that return value, I could call a withPhone method and 44 00:02:26,206 --> 00:02:27,924 specify the phone number. 45 00:02:31,753 --> 00:02:33,680 And that should be a long, not a string. 46 00:02:34,820 --> 00:02:40,670 And finally, I'd call a build method to build my final contact object. 47 00:02:40,670 --> 00:02:45,250 And actually, without too much effort, our code could look like this. 48 00:02:45,250 --> 00:02:49,999 So, let's head over to contact.java to see how we can accomplish this. 49 00:02:51,090 --> 00:02:54,320 The first thing we'll need to do is, add a contact builder class. 50 00:02:54,320 --> 00:02:56,400 And though we could create this in a separate file, 51 00:02:56,400 --> 00:02:59,760 we can also embed a static class in the contact class. 52 00:02:59,760 --> 00:03:03,080 So for simplicity here in this course, that's what we'll do and 53 00:03:03,080 --> 00:03:04,440 I will do this at the bottom. 54 00:03:05,750 --> 00:03:10,700 Public, static, class, contact, builder. 55 00:03:12,260 --> 00:03:14,300 We'll need a field in this class for 56 00:03:14,300 --> 00:03:17,320 each of the contact fields we want our builder to configure. 57 00:03:17,320 --> 00:03:18,970 So let's add those now. 58 00:03:18,970 --> 00:03:22,890 I'll scroll up, and copy and paste the four that I'm interested in. 59 00:03:22,890 --> 00:03:27,820 From above, so I'm interested in these four, right here. 60 00:03:27,820 --> 00:03:32,658 So, I'll paste those right here and I'll remove my JPA annotations. 61 00:03:39,055 --> 00:03:42,265 Great, now the contact builder constructor will contain 62 00:03:42,265 --> 00:03:44,170 the two fields that are required. 63 00:03:44,170 --> 00:03:48,010 And then, we might assume everyone will get right away, the first name and 64 00:03:48,010 --> 00:03:49,030 the last name. 65 00:03:49,030 --> 00:03:52,860 Of course, this assumption could certainly be up for debate. 66 00:03:52,860 --> 00:03:57,760 Let's create that constructor now, public contactBuilder. 67 00:03:59,330 --> 00:04:04,500 And in there, I'll specify the first name and the last name. 68 00:04:05,860 --> 00:04:09,261 And this like any other constructor, will initialize its fields. 69 00:04:15,093 --> 00:04:19,775 And now, we get to add those handy readable methods that we so desired. 70 00:04:19,775 --> 00:04:22,013 Here is the withEmail method. 71 00:04:22,013 --> 00:04:27,595 I going to declare it as a public ContactBuilder method withEmail and 72 00:04:27,595 --> 00:04:32,410 as a parameter, we specify the email address. 73 00:04:32,410 --> 00:04:35,280 Now this method is sort of like a setter, in that, 74 00:04:35,280 --> 00:04:38,660 we set a field using a given parameter value. 75 00:04:38,660 --> 00:04:43,840 But, there's one glaring difference, which makes this builder pattern so 76 00:04:43,840 --> 00:04:46,870 attractive and that is the return value. 77 00:04:46,870 --> 00:04:50,910 Notice that we're returning a contact builder object here and 78 00:04:50,910 --> 00:04:53,330 the return value is this. 79 00:04:53,330 --> 00:04:57,130 The object on which the method was called, a contact builder object. 80 00:04:57,130 --> 00:05:00,300 This is the part that allows us to chain method calls. 81 00:05:00,300 --> 00:05:05,090 If I pop back over to application.java, I see these chain method calls. 82 00:05:05,090 --> 00:05:09,110 I create a contact builder and on that builder, I create with email and 83 00:05:09,110 --> 00:05:14,865 I chain that to a withPhone call and finally a buildCall. 84 00:05:17,380 --> 00:05:22,580 So in a similar fashion, let's now code the withPhone method. 85 00:05:22,580 --> 00:05:27,280 So below the withEmail, I'll create a public ContactBuilder method. 86 00:05:27,280 --> 00:05:31,920 Call it withPhone, and as a parameter, I'll include a long 87 00:05:33,460 --> 00:05:39,280 and again, like a setter, we will set the field using the parameter value. 88 00:05:39,280 --> 00:05:42,090 Except that we, as opposed to a setter, 89 00:05:42,090 --> 00:05:45,030 will return a contact builder object, this. 90 00:05:46,110 --> 00:05:49,390 Now the final method we need to create in this class, is the build method. 91 00:05:50,850 --> 00:05:51,930 So to do that, 92 00:05:51,930 --> 00:05:56,910 this one will create the contact object that we are after in the end. 93 00:05:56,910 --> 00:06:02,280 I'll call this build and we'll return a new contact object and 94 00:06:02,280 --> 00:06:07,000 give the contact constructor a reference to this object. 95 00:06:07,000 --> 00:06:12,520 And I can't forget to create the object by using the new keyword. 96 00:06:12,520 --> 00:06:15,490 Now, since we don't have a constructor that accepts a contact builder 97 00:06:15,490 --> 00:06:18,330 object as a parameter, we need to create that as well. 98 00:06:18,330 --> 00:06:21,010 So, we'll scroll up here and create that. 99 00:06:21,010 --> 00:06:24,280 I'll do that after my default constructor that I listed here. 100 00:06:24,280 --> 00:06:29,630 Public contact and this will accept a contact builder object here. 101 00:06:29,630 --> 00:06:32,390 I'll just name it builder as the parameter name. 102 00:06:32,390 --> 00:06:33,570 And in this constructor, 103 00:06:33,570 --> 00:06:38,530 we'll initialize all the contact fields with the builder field values. 104 00:06:38,530 --> 00:06:46,661 So to do that, I'll start with this.firstName pools builder.firstName, 105 00:06:46,661 --> 00:06:51,475 this.lastName Equals builder.lastname. 106 00:06:51,475 --> 00:06:56,305 This.email equals builder.email, and 107 00:06:56,305 --> 00:07:03,290 this.phone equals builder.phone and there you have it. 108 00:07:03,290 --> 00:07:06,630 If you want to prove this works in application.java, 109 00:07:06,630 --> 00:07:10,980 you can print the contact object to standard out and run the application. 110 00:07:10,980 --> 00:07:14,100 Now, I see that I'm missing an import statement here, so I'll go ahead and 111 00:07:14,100 --> 00:07:15,760 import that. 112 00:07:15,760 --> 00:07:19,750 If you get this prefix of Contact.ContactBuilder, 113 00:07:19,750 --> 00:07:25,680 that is because ContactBuilder is a static class in the contact class. 114 00:07:25,680 --> 00:07:28,000 If you don't want your code to look like this, 115 00:07:28,000 --> 00:07:31,612 you can simply alter the import statement above. 116 00:07:31,612 --> 00:07:35,710 You can, in addition to importing this class, 117 00:07:35,710 --> 00:07:39,800 you can import ContactBuilder explicitly. 118 00:07:40,860 --> 00:07:44,060 So below this initialization of the contact object, 119 00:07:44,060 --> 00:07:47,550 I can simply display the contact object. 120 00:07:47,550 --> 00:07:53,610 Which willll call that objects to string method, which we included in our class. 121 00:07:53,610 --> 00:07:56,980 Before I run this, I'm going to comment out the session factor. 122 00:07:56,980 --> 00:08:02,360 Since we don't want to initialize all that hibernating functionality quite yet. 123 00:08:02,360 --> 00:08:06,330 So with that commented out, I'm going to right click application and 124 00:08:06,330 --> 00:08:11,700 choose run and let's see what our output contains. 125 00:08:11,700 --> 00:08:16,970 There are the results of the Contact's two string method, I see I got an ID of zero. 126 00:08:16,970 --> 00:08:20,190 That will be the default value of that long field. 127 00:08:20,190 --> 00:08:25,970 As well as a first name, last name, email address and phone number that I specified. 128 00:08:25,970 --> 00:08:28,240 It looks like our builder pattern worked. 129 00:08:30,520 --> 00:08:32,390 Now you've seen the builder pattern. 130 00:08:32,390 --> 00:08:35,660 Which is often used for objects that involve complex and 131 00:08:35,660 --> 00:08:38,590 otherwise, unintuitive configuration. 132 00:08:38,590 --> 00:08:42,100 For more on Java design patterns, check the teacher's notes. 133 00:08:42,100 --> 00:08:45,570 But now back to our regularly scheduled hibernate program.