Abstract Factory Design Pattern
An abstract factory like a factory creates objects or instances of something that are not it. The difference is that an abstract factory doesn't refer directly to what it is a factory of besides implementation of an interface. This is very handy when the actual objects that will be registered can be vast. In this lecture, you will learn the differences between a factory and an abstract factory.
Video Transcript
00:00The next design pattern that we're gonna confront
00:02is the abstract design pattern or the abstract factory
00:06that is really an extension of the factory.
00:09So what is different with the abstract factory?
00:12The abstract factory is really needed
00:14when you're trying to make an implementation
00:17of a factory that is more dynamic.
00:19The idea behind there is to be able to add more features
00:23more easily to the factory without needing
00:25to modify the factory again.
00:27This is really only logical
00:29when you're expecting those type of behaviors.
00:32Moreover than that,
00:32you really wanna avoid over complication.
00:35So if you're thinking should you create a factory
00:37or an abstract factory,
00:39most chances are that you just need to create a factory.
00:42You'll only really create an abstract factory
00:44after you created a factory and saw
00:46that you just need more out of it
00:48and then you might mature out of that factory
00:51into an abstract factory.
00:53So let's jump right into it
00:54and let's modify our factory
00:56and turn it into an abstract factory.
01:00In JavaScript, there is no such thing as interfaces
01:03but there still is the concept of an abstract factory.
01:08Now the real idea behind an abstract factory
01:11is a factory where the creation process
01:14is invisible to the factory.
01:16What is it actually creating
01:18beyond the implementation of an interface?
01:21But because there are no interfaces,
01:22we're gonna have to do it a little bit differently
01:24in JavaScript.
01:26So the first thing that I'm gonna do
01:27is I'm gonna have to change the structure
01:29of our circle creation from a function
01:32to, from basically a function object
01:34to split it down into actually the steps
01:37involved with the creation.
01:39So to do that, I'm gonna go ahead here
01:40and just I'm gonna rip this out
01:43and instead of, you know, we don't even need this var
01:45and instead of that, I'm just gonna go ahead
01:47and create that function.
01:49I'm gonna do this one by one to all of them.
01:52So in the creation process of this constructor,
01:54I'm probably not gonna have anything in it currently.
01:57I might wanna add just for the sake of, well, no,
01:59there's just nothing really I need to add in here.
02:02But I'm gonna continue on.
02:03Once I've created my function,
02:05I'm gonna go ahead and use a prototype.
02:07So I'm gonna go to that red circle,
02:09create a prototype.
02:10Oopsie, there we go, prototype and our method name,
02:14we're gonna call this create
02:16and I'm not gonna put here anything here for this one
02:20and I'm just gonna go ahead and assign a value
02:22to this item, which is literally almost the same
02:25while we were breaking it down into another step.
02:28All right, so I'm gonna do the same thing now
02:30for the blue circle.
02:32I'm just gonna go ahead here and just delete that,
02:35set that right there.
02:36I'm just gonna go ahead and copy this
02:38so we could do this a little bit quicker
02:41and I'm gonna go ahead here and just copy this definition
02:44of the item itself and literally what I'm doing here
02:47is I'm defining the value of the item.
02:50I could go ahead also and just set something,
02:52for example, in the prototype
02:53because I know there's always gonna be an item
02:55but I'm not gonna do that.
02:56This is gonna be an instance reference
02:58or an internal reference.
03:00I might have your create process followed by get maybe,
03:04for example, but I'm gonna keep it simple for now.
03:06So we're only creating the object itself.
03:09Once created in my factory,
03:11what I wanna do is instead of creating items directly
03:15by listing out the items,
03:17I wanna go ahead and add one more method.
03:19And the method that I'm going to add
03:21is going to be a method that's called register.
03:24Now, the idea behind here is literally
03:26to take out the step of the knowledge of what the item is
03:30to an external library,
03:31in our case to the circle generator singleton.
03:34The reason is because what if we have usage
03:37for these circles in completely different scenarios?
03:40So we might need different circle factories.
03:42Maybe we have a few circles in our application
03:44that are used in a completely different set
03:46using different objects, using different registers.
03:49So I'm gonna go ahead and just set here the type
03:51and I'm gonna set a class as the properties.
03:55Once I do this, I could go ahead and test for my interface.
03:58And again, because there is no such thing as an interface,
04:01I know that anything that comes in here
04:03must have a prototype of create.
04:06So I'm gonna go to that class and ask for the prototype.
04:10And in the prototype itself,
04:11ask do you have the create function?
04:14Now, if you have the create method, not clear create,
04:17great, now we're gonna register.
04:19So only if it exists, then I'm gonna go ahead
04:21and to the next step, I'm gonna go ahead
04:23and actually define some sort of an object
04:26that will store all the types.
04:28And I'm just gonna create here an object.
04:30And then in the register itself,
04:32I'm gonna go ahead and say, okay,
04:33this dot types with the type name,
04:37let's go ahead here and define it to be that class.
04:40So we could register and then use it
04:42in the creation process.
04:44Now that we have a register,
04:45I could go back to my creation process
04:48and I'm gonna go ahead and also change this to be type
04:50to make it more generic,
04:51just like the names that we have down below.
04:53And I'm gonna go ahead and actually define it by returning.
04:57What am I gonna return?
04:58I'm gonna return a new this dot types type.
05:05Now I'm fine with having an error here
05:07if the type is not defined
05:08because then the user will see
05:09that they made a mistake there.
05:11And I'm gonna go ahead here and just delete all this.
05:13Literally what I did here is I turned my circle factory
05:17into an abstract circle factory.
05:19And because we don't have interfaces in JavaScript,
05:22I tested for the interface through this if condition,
05:25checking to see if it's implementing the interface
05:27that is needed for something that would be called a circle.
05:31A circle, circle.
05:33All right, now that we have our prototype,
05:35now that we have our abstract circle factory configured,
05:39it's time for us to go back into that circle factory.
05:42But this time around, once we've created it,
05:44I'm gonna go into my CF, my circle factory,
05:46and I wanna register more items.
05:49And I'm gonna go ahead and first register the red item.
05:53And the red item is gonna accept a red circle.
05:56And I'm just gonna go ahead here and duplicate this
05:58because we wanna do the exact same thing
06:00for our blue circle.
06:01But look what I've done here.
06:02Because I broke this down, I could very easily decide,
06:05hey, I wanna create a green circle,
06:08or even something that's not even a circle.
06:10For that matter, you know,
06:11I might wanna change the name of the circle factory.
06:14All right, so now that I have these registers,
06:16all that's left for me to do,
06:17and I'm just gonna go ahead and change these names
06:19from color to type because type makes more sense.
06:23And once the item is created,
06:25I wanna go ahead and check this.
06:27So once the item is created and called from our create
06:32method here, we're only creating the item,
06:35but we're not actually triggering the creation process.
06:39So I'm gonna go ahead and automatically,
06:41once it's actually instantiated,
06:43I'm gonna go ahead and create the item
06:45because if it's being constructed,
06:46I also wanna create it at that stage.
06:49So we have only one more thing left to do,
06:51and that is because we're calling
06:53the create method right here,
06:55we're gonna make sure that we're actually
06:56gonna return the item itself in our process
06:59so we don't have to separate the steps.
07:01That way, we're still gonna return back that full object,
07:05that red circle or blue circle that we've created.
07:10Red circle, oh, and here we go.
07:11I see a little error here that I forgot
07:13to update the blue circle, so I've just done that as well.
07:16Let's just take a peek before we test everything out.
07:19Let's save this.
07:20So this looks to me like it's good.
07:22So let's go ahead and give it a test
07:23and see if it's working.
07:25And we should still be able to, here we go,
07:27we could still be able to add things
07:28the way we've done before,
07:29or by clicking on the A, uh-oh, we have your little issue.
07:33All right, let's fix that.
07:34Line 47 seems like is not, oh,
07:36because I only returned for the create of the red circle,
07:41not the blue circle.
07:42So let me go ahead there and update that.
07:45Click on refresh.
07:46So again, so we have here for the clicks
07:47for the red circle that's working.
07:49And when I click on A with the keyboard,
07:51that's working as well.
07:52So let's just wrap this up.
07:54What I basically did is I turned our original circles
07:59into objects with prototypes.
08:01I've done that because I needed to have an interface
08:04so I could then, in the process of the creation
08:06in the factory, I could register those items
08:09and test out to see if they're implementing
08:11the interface that is mandatory
08:13for anything that would be considered a circle.
08:16Once that was configured and we had,
08:18and we updated our factory to register elements,
08:21we didn't have to list them out
08:22because we could actually add them in dynamically.
08:25This enables us now to use the circle factory
08:28in a much more robust way
08:29because then if there are more circles or more items
08:33or more factories, I could reuse the same factory
08:35because it's not referencing directly
08:38to what it's implementing.
08:40Last but not least, we had to go ahead
08:42and add a few lines in wherever we created our factory
08:45and list out all the different items that it will support.
08:49Once we had that configured and set up,
08:51we could then start running our application again.
08:53Now, again, one of the processes
08:55and one of the maybe negatives here
08:58is that I'm, again, listing out manually
09:00all the different items that could be inside,
09:03which is probably a negative here.
09:05What I might wanna do is I might wanna send this factory
09:08in maybe if I wanted to extract it to give me more control,
09:11literally creating the factory down below here
09:14somewhere in our code,
09:16making auto our circle generator a lot more flexible
09:20because at this stage, because we hard coded it inside,
09:22it won't be as flexible.
09:23So that could be an assignment for you to do,
09:25but we're gonna continue on.
09:27We're not gonna focus on making things the most optimized
09:30because really our goal right now
09:31is to try to make it very clear to you
09:33the difference between a factory and an abstract factory
09:37and I think we've just done that.
09:38In this lecture, we modified our factory
09:41and turned it into an abstract factory.
09:44The main feature of a factory was
09:46that it didn't really know what it's implementing.
09:49Even if it listed it out,
09:50it would always do it in the exact same way.
09:53Contrary to that, our next design pattern
09:55that again is an extension next step
09:57of the current factory is the builder.
10:00So we'll see you in the next lecture
10:01when we explore the builder design pattern.
