Node js Design Pattern, The Prototype pattern

When I was growing up, I noticed that the layout of our house was very similar to the layout of several of my friend’s houses. They were the same size, they had the exact same floor plan and they were all made of the same materials. We essentially lived in the same house, but each house had customizations that made it different.

It seemed like whatever company that built these houses had a pattern they can follow to make them more efficient by using the same materials and blueprints. Each house was essentially a clone or a copy of a master design. Sometimes we face the same challenge in software development.

We might spend a lot of time creating and setting up objects that are essentially the same.Let’s take a look at the index.js file found in the start folder of our exercise files under chapter two, lesson four. We’ll notice that we are using a shopper class to create two instances of shoppers.

On line three, we create a shopper named Alex and on line 10, we create a shopper named Eve. Both of these shoppers actually have essentially the same list, so the process of setting up each shopper requires that we duplicate the same code. Both Eve and Alex have a camping knife, a tent, a backpack and a map.

So what we can do is we can save time and reduce redundancy by implementing the prototype pattern. So to do so, I’m actually going to copy everything from line seven to line one. So we’re going to copy the setup of the shopper Alex and then I’m going to create a new file called scout_prototype.js. We’ll go ahead and paste this information here. So in the scout prototype, we will create a new shopper called a scout and we will add all of these items to the scout’s list. We’ll also remove the scout’s name because we’re going to use this instance of this object to create multiple scouts.

So the last thing that I want to go ahead and do here is export our scout object. So now we have an object that we set up that has a camping knife, tent, backpack and map already in their shopping list. Let’s go back to the index.js and use this item to improve our code. So instead of importing a shopper, we’re going to import the scout prototype, prototype. And now instead of creating Alex using the shopper, what we’re going to do is use the scout prototype. And by using the scout prototype’s clone method, we create an exact clone of the scout prototype in its current state.

That means that Alex already has all of these items on their shopping list. And I can simply add a slingshot to my shopping list. Now I do have to give this instance a name, so we’ll say equals Alex Banks and we want to do the same thing for Eve, so I’m going to go ahead and copy our scout_prototype.clone call and set Eve using that call too. So, the instance of Eve will again be the scout prototype at it’s current state, so we’ll go ahead and get rid of that list because Eve will automatically have it and the name Eve Porcello.

So you can see that we’ve improved our code, we’ve eliminated the redundancy. If we had very complicated objects to set up this could save us a lot of time. Now unfortunately we aren’t ready to run this code yet because we haven’t implemented the clone method. In the next lesson, we’re going to go ahead and add the clone method to our shopper class, so that we can use the prototype pattern to speed up the way that we create instances of shopper objects.

Leave a Reply

Notify of