02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Factory Design Pattern

a factory enables us to separate the steps involved from something that we need to be customization and interchangeable from the core logic of our application. We will use factories only when there is a high likelihood that the creation process will change throughout the life of the application. In Javascript there aren't abstract factories but just factories.

Video Transcript

00:00The next design pattern that we're gonna tackle
00:03is the factory design pattern.
00:05One of the unique attributes of the factory
00:08is that it enables us to have more control
00:11of customizing the item.
00:13And the idea behind here is basically extracting
00:16the item creation from the object that's implementing it,
00:20making the implementing object not doesn't,
00:23making it basically unaware of how is that object built,
00:27making it much easier for us to then inject other objects
00:30without needing to go back into the original object
00:33and make changes in it.
00:35Beyond that, it makes it really easy to make updates
00:37and add more versions or updates to the object
00:40without again needing to go into that original implementer
00:43because the implementation itself doesn't know
00:46how it's actually created
00:47because it's sending it out to a factory
00:49that's creating it for it.
00:51So let's jump right into it
00:52and let's see a factory in action.
00:55Okay, now I'm gonna, the goal here,
00:58and it's really a simplistic goal,
00:59I'm gonna go ahead and whenever I'm typing
01:01with the A character to add items,
01:03I want the items to be blue.
01:05Whenever I'm clicking with the mouse,
01:07I want them to be red.
01:09Now, I definitely don't wanna add any complexity
01:12or any logic within our circle generator.
01:15What I wanna do is I wanna extract the process
01:18of the creation itself from outside of the singleton
01:21and literally create the two different implementations.
01:25And that way we're gonna use a factory
01:27which will enable us very easily to add variations
01:29without needing to go back into the singleton
01:32once we've implemented it.
01:34So I'm gonna go ahead and define a new variable
01:36and I'm gonna start with a red circle.
01:40And I'm gonna go ahead and I'm literally gonna go ahead
01:42and just create here just an object
01:45and this object is literally just gonna have an item
01:48and its item is just gonna be a reference
01:51to that original creation process that we had previously.
01:55So there we go, we have here our red circle object
01:58and I'm gonna go ahead and immediately go ahead
02:00and create a blue circle.
02:02And I'm just gonna use some little playing around here
02:05so I'm not gonna do it with CSS,
02:08well, not built in CSS, I'm just gonna go ahead
02:10and just add just so,
02:12because we just wanna sample the differences
02:14and I'm gonna put here a background
02:16and I'm gonna set it to be blue.
02:18All right, so now I've created two unique implementations
02:21of that red circle and blue circle.
02:25The next step is I wanna create a circle factory
02:28and again, the circle factory could have
02:30an endless amount of circles.
02:32The most important thing is that the interface,
02:34the public interface will be the exact same
02:37between the red circle, blue circle
02:39and any other circle we would add.
02:41In this stage, I didn't use the module
02:45but I should, especially if I had private
02:48and public properties but for the sake
02:49of just showcasing what a factory is,
02:52I'm gonna skip the process of creating
02:54this public and private constructs.
02:56We'll do that at a later date.
02:58So let's continue and create that circle factory.
03:03And our circle factory is also gonna be an object
03:06and this object is gonna have basically
03:08only one method at this stage
03:10and that one method is gonna be this.create
03:14and the create is gonna basically ask for a color
03:18and based on the color, it's gonna send in
03:20the actual implementation of the right one.
03:22Is it gonna be the blue one
03:24or is it gonna be the red one?
03:26So in this case, I'm gonna ask if the color
03:29is exactly equal to blue, then and only then,
03:33go ahead and return the blue implementation
03:37by creating a new blue circle.
03:40Beautiful.
03:41In any other case, I'm gonna go for the red
03:43because there's only two implementations
03:44so even if I set the wrong value there,
03:47I'm gonna send back a new red circle.
03:51Beautiful.
03:52So now I have the create factory,
03:54a circle factory as well.
03:56So again, I created an actual instance of a red circle,
04:01an object to be created for the blue circle as well
04:04and then I created a circle factory
04:06that is gonna manage which item
04:08is gonna be actually created next.
04:10The reason why we're separating this here
04:12and we're not implementing it exactly
04:14where we want a blue item or a red item
04:16is because we wanna have an easier way
04:18to manage it in a central place
04:20so we don't have these configurations
04:22all over throughout the application
04:24and the only difference will be the value
04:26that we're sending in for the creation process
04:29in the create circle factory itself.
04:33Now that we configured our factory,
04:34the next step is to actually go inside
04:36of our singleton in our case
04:38and I'm gonna go into the init
04:40where I actually wanna create a reference
04:42to that circle factory,
04:45literally creating a new circle factory.
04:48Once I've created that new circle factory,
04:50now I have access to generate new circles.
04:53So I don't need to create the circle anymore directly here
04:57but instead of that, I'm gonna call the create method
04:59and send inside the color
05:01that will be passed through as a property.
05:04Now again, if I sent the wrong color,
05:06it will just return red so that should continue working.
05:09Last but not least,
05:10because I need direct access to the item itself,
05:12I'm gonna approach the item directly
05:15and now I have access directly to the item.
05:18All that's left for me to do
05:19is to go to any place where I'm creating
05:22and then add one more property.
05:23So in this case, whenever I have here a click,
05:26I want it to be red
05:28and whenever I'm gonna add here something using a keyboard,
05:31I'm gonna use the blue.
05:33So let's go ahead and give this a whirl
05:35and give it a test.
05:36I'm gonna click here on refresh.
05:37If I click around,
05:39I'm gonna get red items
05:40and if I press on my A,
05:42then I'm still getting red.
05:43So let's see why am I still getting red.
05:45First of all, it's working.
05:46So that's already a good sign.
05:47Only bad sign is that we're actually not getting blue.
05:50So might be that we have here some sort of error here.
05:53Let me just break a line.
05:54Blue is the value here.
05:55Am I assigning the value inside of my constructor
05:58to be blue exactly?
05:59No, I have your extra space that was wrong.
06:02So let's give it one more shot
06:04and if I click around, it'll be red.
06:07If I press on the A,
06:08then I'm gonna get my blue circles
06:10and there we go.
06:11We've just implemented our first factory.
06:14In this lecture, we met the factory
06:16and we've seen it in action.
06:18The most natural step right now
06:19is to move on into the abstract factory
06:22that enables us even to make our factory itself
06:25unaware of what is it actually creating
06:27beyond an implementation of an interface,
06:29which we'll look at in the next lecture.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!