Prototype Design Pattern
The prototype is one of the most natural design patterns that are built into the language.In this lecture, we will see hands on how easy it is to implement this design pattern in JavaScript. The main concept is duplicating methods for reuse instead of creating new ones. This is very effective way of reducing memory usage as well as we are not creating a new method but creating a link to it.
Video Transcript
00:00We're ready to confront the last design pattern
00:03in our creational design patterns
00:05and that is the prototype design pattern.
00:08One of the, it's one of my favorites really
00:10because it's so easy to implement in JavaScript
00:13and it's also very memory efficient.
00:16It's memory efficient and you're gonna see this
00:18in the lecture itself mainly because
00:20we're only referring to functions and we're using them.
00:23We're not using inheritance
00:25which is then duplicates those functions.
00:28Instead of that we're just referring to those functions
00:31and reusing them and basically we're very, very efficient
00:34with our usage, our methods that way
00:36and it also creates a lot of flexibility.
00:38And one of the greatest advantages is this is built
00:41into the language itself and again
00:43it's very easy to implement.
00:44So let's jump right into the prototype design pattern
00:47and see its wonderfulness in action.
00:51With the prototype design pattern
00:53our goal is to take advantage of the fact
00:56that JavaScript is a prototype based language.
00:59I'm gonna go ahead here and create a function called clone
01:02which is really gonna be the heart of the cloning process.
01:05And what I'm gonna do here,
01:06I'm gonna define myself a source
01:08and I'm gonna design an outcome.
01:11So I'm gonna sign out in so that is gonna be our outcome
01:15and what I wanna do is I wanna loop through every attribute
01:19inside of our source prototype
01:24and what do I wanna do with it?
01:25I literally, what I wanna do is I wanna define it
01:27or push it into our new object that we're creating.
01:33Literally what I'm doing is I'm literally out prototype
01:37and I'm adding it into that same attribute.
01:40I'm just going ahead there and just defining
01:43and adding inside of it the actual content
01:46of the source itself.
01:48So let me just go ahead there and just paste that
01:50and I'm literally, what I'm doing is I'm duplicating
01:52the data that is inside of our source
01:55into our new object.
01:56Okay, so this is the basic, the core foundation
01:59of the prototype design pattern.
02:02Now that we've created it
02:03and this is all we really needed to create,
02:05let's go ahead and actually implement an example of it.
02:08So I'm gonna go ahead right underneath our circle
02:11and I'm gonna create a rectangle.
02:13So I'm gonna go ahead and I'm gonna call it,
02:15this function, I'm gonna call it rect.
02:17Now the rect function is gonna do almost the exact same
02:20thing as the circle does in its constructor
02:22by creating a new circle
02:24but instead of creating a new circle,
02:26we're gonna create here a new rectangle
02:28and I already have that in my CSS pre-configured.
02:31The next step after this, what I'm gonna do
02:33is I'm gonna literally go and ask to clone
02:36all the behaviors, clone, how do I call this?
02:43Clone, yeah, there we go.
02:44Something here is not getting colored.
02:46Oh, because it's had an O in the beginning.
02:48Okay, so clone and what do I wanna clone?
02:50I wanna first, I wanna clone the circle
02:53and I wanna clone it into my rectangle.
02:55Literally all the different prototype methods
02:57such as the capability to color it and to move it
03:00and to get it is gonna be right there
03:03inside of our rectangle because I just cloned away.
03:06Now this can become incredibly powerful
03:08if you just wanna have maybe a helper,
03:10instead of putting this in the circle,
03:12you would have maybe a colorizing or styling object
03:15and you would have a moving object
03:16and you have other different scenarios
03:18and then you could plug in and add to different objects,
03:20features and capabilities based on need.
03:22For example, resizable, for example, movable
03:25and then you could just basically clone
03:27those features into them as you see fit as you need them.
03:30So let's see it in action.
03:31To be able to see it in action, what I'm gonna do,
03:33I'm gonna go into my blue circle builder
03:35and I'm gonna go ahead here and just create
03:37a new instance of that rectangle
03:40and I'm gonna go ahead here and create a new rect.
03:43Notice this is a perfect example of a usage of a builder
03:47because maybe I'll put this in the initialization
03:51just for the sake of keeping with a logical construct here.
03:55So okay, so I'm creating a new rectangle
03:57and with my rectangle, I could go ahead,
03:59you're in sale, let me color it.
04:02So I'm gonna color it yellow.
04:04What else do I wanna do here?
04:06I wanna color it yellow and once I've colored it yellow,
04:09I also wanna move it.
04:10So I'm gonna move it to a new spot.
04:12Let me move it to 40 40 and last but not least,
04:15I'm gonna go ahead into this dot item
04:19and I'm gonna go ahead and I'm gonna auto add it.
04:23I believe that was the method.
04:25Oh no, I'm gonna get the jQuery reference
04:28and then I'm gonna go and append it right inside.
04:31So I'm gonna take that rectangle,
04:33get that reference of that jQuery item
04:35and what I'm doing here is I'm basically
04:37appending it and adding it into that initial color object.
04:40This is just for the example of
04:42the power auto of the builder.
04:43So we're taking advantage of more facts
04:46that we've learned throughout this title.
04:48Okay, let's go ahead and test it out and see if it's working
04:50and if everything is working according to plan,
04:53anytime I add a blue rectangle, a blue circle,
04:56now it's gonna have a nice yellow rectangle
05:00connected to it.
05:02In this lecture, we saw the prototype and with it,
05:04we wrapped up our creational design patterns.
05:07In the next section, we're gonna move on
05:09to a completely new group of design patterns
05:12which are called structural design patterns.
05:15So we'll see you in the next section
05:16as we explore structural design patterns.
