Builder Design Pattern
The builder design pattern is in many ways a continuation of the Abstract Factory design pattern. The builder enables us to create complex shapes split into steps and even merging more then one object with each other to create the outcome that we need. Most of the time you would start with a Factory, as it grows old move it into an Abstract Factory and only if at that stage it's logical to break it through into a builder you would move into a builder.
Video Transcript
00:00As you probably noticed, each step we took
00:03in the last few lectures brought us to a more complex
00:06and more complex design pattern.
00:08And we're still gonna go to yet again,
00:10a little bit more complex with the builder design pattern.
00:14Now, the builder design pattern is very similar
00:17to a factory limit to the fact that it actually does
00:20a lot more than just one thing.
00:22Take for example, the jQuery dollar method,
00:25which is a builder and how it creates a lot of things.
00:28How does it do it behind the scenes?
00:30And that's exactly what we're gonna learn
00:32in the process of creating our own builder.
00:35Now, builders are really a thing that we wanna do
00:37whenever we need to group up and do things
00:40with multiple objects to create something
00:42to be used by another object.
00:45Or if we have a lot of steps that we need to take part
00:48or we need to share a lot of steps
00:50that then the object will have to take care of.
00:52So those are really the combinations
00:53of when we would use a builder.
00:55Now, that seems a little bit theoretical right now,
00:57but fear not because we're gonna jump right into it right now.
01:02All right, so let's continue.
01:03You now know how to create a factory and an abstract factory.
01:07The next thing is a builder.
01:09And to be able to create a builder,
01:10I'm gonna go ahead first and create a core data type
01:14that will be just a circle.
01:16And I'm gonna go ahead here and create that circle.
01:18And the circle prototype, I'm gonna go ahead here
01:21and literally list out all the things circles could do.
01:25Now the idea behind here,
01:27and we're gonna get to the part of the builder
01:29once we have basically this all can do circle
01:32that could take care of its own stuff.
01:34So what things do circles do?
01:35They create, they get created.
01:38All right, fair enough.
01:39So we're gonna, again, I'm gonna just literally,
01:41I'm just gonna copy the core logic
01:43from within our creation process.
01:45So first of all, a circle could be created.
01:48What other things could we do with circles?
01:49Let's look into our circle factory or our circle generator.
01:53So we could see first of all
01:55that we have here a few steps.
01:56One is that we could position circles.
01:59So let's go ahead and also add a position method.
02:02So I'm gonna go ahead here
02:03and I'm just gonna very quickly,
02:05let me just even steal it from our position logic here.
02:09And I'm just gonna quickly grab that.
02:11And I'm gonna go ahead and just add
02:13into my prototype chain of circles
02:16that circles, what could they do also?
02:17They could be also positioned or we could move them.
02:20Let's call it move
02:21to make it a little bit more interesting.
02:24So that will be our left, our top.
02:26And this time around, we would only approach
02:29instead of that our actual item itself.
02:32All right, beautiful.
02:33So I'm gonna go to my item itself.
02:34And by the way, the create process itself
02:37does not make any sense anymore
02:38because we need this item as soon as it's created.
02:41So as soon as a new circle is made,
02:43we need to assume that our item has been created.
02:46So I'm gonna go ahead
02:47and just push that right into that circle itself.
02:49So the creation of the circle itself
02:52is already the actual creation.
02:55All right, so next step,
02:56let's just go ahead here and equal function.
02:58Let's configure that circle up.
03:01Oopsie.
03:02And let me just lay this out a little bit better.
03:04All right, so we know that we could also move items.
03:07The creation is in the constructor.
03:09The moving is that positioning
03:12that happens within our singleton.
03:14Let's go to the singleton
03:15and see what other things does that singleton circle,
03:18does a circle do?
03:19We also add circles into something,
03:22but we're not gonna add that into the circle itself
03:24because we don't wanna actually add something like that
03:27because we don't wanna put inside of the circle
03:29something that is relevant to something that is out of it.
03:32So we're not gonna approach anything
03:34that has to do externally to the circle.
03:37But beyond that, we could also add here
03:38maybe one more thing
03:39and we could add into our circle also maybe a coloring.
03:42So I'm gonna go to circle, prototype,
03:47and I'm gonna call this color
03:50or maybe let's call this even tint.
03:53And I'm gonna tint this with a function
03:56and it's gonna be just a CLR for a color.
03:59Why did I call it tint then?
04:00So let's call it color.
04:02All right, so we're gonna call it color.
04:04And what do we do in this scenario?
04:05We could go to this item,
04:07dot item,
04:09that CSS,
04:11and what property we wanna set.
04:13We wanna set the background.
04:14What color do we wanna set?
04:16We wanna set that CLR color.
04:18So there we go.
04:19We could put any color really in a circle,
04:21creating a much more sophisticated circle
04:23than we ever have done before.
04:26The next step now that we have a core beta prototype
04:29for our circle,
04:30it's time for us to create circle builders.
04:33And for my circle builder,
04:34what I wanna do is I wanna basically create here,
04:36and this is really the scenario.
04:38I'm not gonna create a red circle anymore,
04:40but instead of that,
04:41I'm gonna call this a red circle builder.
04:47And for our red circle builder,
04:48literally what we wanna do is a few steps.
04:51First of all, every single circle,
04:52no matter what it is,
04:54needs to have a circle.
04:55So I'm gonna go ahead and automatically create a new circle,
04:59listing out, making sure that any time a builder is created,
05:04a circle was created.
05:06And maybe our next step would be maybe to be set up,
05:10or I'll call this initialize.
05:13And in our initialize process,
05:14what do we wanna do for the red button?
05:16Not much.
05:17Our circle is already red,
05:19so I'm not gonna do nothing, okay?
05:22Now, for the blue circle on the other hand,
05:24I'm gonna go ahead here,
05:25and what I wanna do again,
05:26I'm gonna create a new circle.
05:28And what's the next step?
05:29I actually wanna initialize that circle.
05:32And in the initialization process,
05:34what do I wanna do?
05:35I actually wanna change the color of my circle.
05:38To do that, I'm gonna approach my circle.
05:41And what do I wanna do with it?
05:42I wanna actually color it.
05:44I wanna color it blue.
05:46Now, obviously the steps here
05:47are probably a little bit more steps
05:49than we would expect to make
05:51in just a basic creation process.
05:53But this really comes handy
05:55only when you have a lot of steps
05:57that a lot of configurations,
05:58a lot of specifications that maybe would be different
06:01to blue circle versus a red circle.
06:03And I'm just gonna go ahead here
06:04and make sure that this is called a builder.
06:07And what we're actually doing here
06:08is this process is basically,
06:11and let me just double check
06:12that everything here is configured correctly.
06:15And for our blue circle builder,
06:20and our blue, there we go,
06:24blue circle builder, there we go.
06:26So we created, first of all,
06:27let's go through this really quickly.
06:29We have here a circle.
06:30It has a few properties of capabilities
06:32of different features of control.
06:35The next, and let me just add one more method.
06:37I'm gonna go to that circle
06:38and I'm gonna add another prototype.
06:40And that prototype will get, we'll call it get.
06:43And all it's really gonna do
06:44is just gonna return to us this return to this item,
06:48giving us access to that circle
06:50to directly to the actual item itself,
06:52which will be used in general.
06:55Now, in our circle builder, besides the initialization,
06:59we might also wanna add here oto a getter.
07:01So I'm gonna go ahead here
07:02and I'm gonna oto add a getter to my builder itself.
07:06And for my builder itself,
07:07I'm literally just gonna,
07:08I'm gonna call this oto get.
07:11And for our get item here,
07:14literally all I'm gonna do is I'm gonna return
07:16my this.item.get.
07:20Literally passing through the actual object itself,
07:23directly the actual implementation of the item itself.
07:26Or if I wanted to,
07:27I could approach and return the item itself,
07:30enabling me to then use the item.
07:32That is in my prerogative of choice.
07:35I'm gonna go ahead and also copy this get process here
07:38and I'm gonna paste it right down there
07:39so we have your two different methods.
07:41Literally what's happened now
07:43is we broke our steps down into one more step.
07:46So anything that is,
07:47what we're actually gonna be implementing
07:49is actually builders.
07:51So for these builders, what I'm gonna do again,
07:53I'm gonna go back into my factory
07:54that's now not necessarily a classic factory
07:58because now we said it's an abstract factory.
08:01And instead of asking for the create to be listed in here,
08:04what I'm gonna do is I'm gonna list out the methods
08:06that we expect from a blue circle builder
08:09or from a circle builder in general,
08:11which is gonna be initialize and get.
08:13So I'm gonna go ahead here and just test
08:14to see that there is an init function.
08:17And I'm also gonna check to see
08:18that there's a class prototype and get.
08:23So these are the two methods that I'm expecting to receive.
08:26Now there is no create anymore
08:27because we're actually returning the whole object
08:29that was created.
08:30And what are we returning right now in our factory?
08:33We're returning actually the builder itself.
08:36We don't really want the builder
08:37because once the build is complete, we're done with it.
08:40And I'm gonna go ahead here and call get,
08:41making sure that I'm actually gonna get a reference
08:44to the implementation of that circle that we've created
08:47that could be red or could be blue.
08:49All right, so now that I've created,
08:51and by the way, in a lot of scenarios,
08:53the builders would not necessarily create one item.
08:55They could create a combination of different things.
08:58Maybe I'm creating some sort of art
09:00that includes circles and triangles and much more.
09:03So the builder itself does not have to return
09:04one distinct item.
09:06It could return a composition of many items together.
09:09In our case, just for the simplicity,
09:11we're gonna leave it at that it's returning one item
09:14or returning something organic
09:16that then we could then use and manipulate down the line.
09:19So we updated our abstract factory
09:21to be able to accept builders.
09:23Now that it's accepting builders,
09:25we could go into our process here
09:28after the creation of what do we actually wanna do?
09:30We don't wanna actually need to register red circles
09:34or blue circles.
09:36Really what we wanna do is we wanna register builders.
09:39So I'm gonna go ahead here,
09:40and I'm gonna add a red circle builder.
09:44So I'm gonna send in that red circle builder,
09:47and I'm gonna send in the blue circle builder.
09:50So those are gonna be registered
09:51as the builders that we've created.
09:54Now that we have those builders configured and created,
09:57really, we're not gonna change much
09:59in our application at this stage.
10:01Although we don't need to access the item anymore.
10:04Instead of that, we're gonna call the get,
10:06although the item should continue working.
10:08But beyond that, now that we have this item,
10:11now that we have this item that is actually
10:13could come back with a lot more logic,
10:15a lot more control, a lot more logic,
10:18I could then go and maybe instead of referring
10:20to the circle directly, I would then maybe refer it
10:23and save it so I could then down the line
10:25whenever I wanna position it,
10:27I could refer back not to a specific circle
10:29as I'm doing right here,
10:31but I could refer to actually specific elements
10:34that are inside of that circle.
10:36We're almost ready to test things out,
10:38but before we do this, one more thing we need to do.
10:41We need also initialize because we could
10:43or do that inside of our factory,
10:45but probably not, because that is something
10:46that should be done inside of a builder.
10:49Because a builder, as we were saying,
10:51the job of a builder is to basically construct stuff.
10:54And usually it was not gonna construct only from one circle,
10:57but it was gonna go ahead
10:58and do a lot more steps beyond that.
11:00So literally what I wanna do is I'm just gonna go to this
11:03and just call the init function
11:04that would basically continue down the line,
11:06both for the red circle and also for the blue circle.
11:10Now, what's really beautiful about this
11:13is that we really don't need to change anything else
11:15in our application besides making sure
11:18that our registration is happening to the builders
11:21instead of directly to the classes.
11:23Once we've changed that and we've set it for our builders,
11:26really everything else should continue working.
11:29And if I go ahead here and just click on refresh,
11:31we should see now that whenever I'm gonna click
11:33on the items, the items will continue working.
11:35And if I press on the keyboard, everything is still working.
11:39Now, again, the advantage here is probably minor.
11:44And you're not gonna see a reason for you
11:45to use it probably right now
11:47if you're new to design patterns.
11:49Usually you're gonna start with a basic factory.
11:52Once you're maturing out of that basic factory,
11:55you'll probably move into an abstract factory.
11:58And once you mature out of the abstract factory,
12:00because now we need to do a composite of a lot of things,
12:03now it's only a circle.
12:04But imagine if we had here,
12:05maybe we were creating an art piece
12:08and there's a few circles that are combined together
12:10in squares and other things,
12:11and there's different combinations.
12:13And we wanted to use those combinations in sync.
12:17That's really the place
12:18where builders will become really useful.
12:21In this lecture, we created the builder,
12:23our most complicated creation to date.
12:26In the next lecture,
12:27we're gonna continue working with the builder
12:29and talking a little bit more in depth
12:31of why are we working with the builder
12:33and the advantages of working with it.
