Learn Node.js design patterns, Implementing decorators

In the last lesson, we defined the decorator pattern, as well as added a couple of decorator classes to some existing code. I’m looking at the exercise files found under Chapter Three, Lesson Eight, within the Start folder.

We added the GoldenInventoryItem and DiamondInventoryItem to this index.js file, and we also created golden necklaces, A Diamond Gold Necklace, and a Diamond Walkman. In this lesson, we need to actually write the codefor the GoldenInventoryItem and DiamondInventoryItem decorator classes.

So let’s go ahead and open up our inventory item file. So we’ll go ahead and add the decorators right here, next to the InventoryItem.

So I’m going to go ahead and add a class GoldenInventoryItem, and a class for DiamondInventoryItem, and we’re just going to export all three of these things together within the same object, so I’ll make sure that I export my GoldenInventoryItem class and DiamondInventoryItem class, right in my module exports. And that’s because we are actually pulling all of these three items from the same InventoryItem module.

So for GoldenInventoryItem, what we want to do is give it a constructor, and the constructor takes in the baseItem. And what we’re going to do is we’re going to add to the existing baseItem. So the golden item’s name will have “Golden” in it. So we’ll take the baseItem name, and we’ll extend it by adding the word Golden in front of it. We’ll also take the price of the item, so whatever the baseItem price is, we’ll just add 1000 to that. So we are extending the baseItem.

It costs a little more, and the description has changed. Let’s do the same thing with the DiamondInventoryItem. So we’ll create a constructor, so again we’re taking in the baseItemand we want to decorate it, so this.name is going to equal Diamond, and the DiamondInventoryItem will also affect the price, so this.price is also going to be another 1000 plus baseItem price. Now, decorators do not have to match the exact same interface, they don’t have to have the exact same fields and methods that our baseItem has.

We can actually add more functionality. For instance, the Diamond can cut glass. So this.cutsGlass is equal to true. We can also overwrite or modify any of the methods. So when we print the diamond, we’ll do something a little different, we’ll go ahead and console log this.name costs a lot of money. So these items are decorators. They take in a base item, or a base class, and they extend its functionality. In this case, we add some more to the price. We change the description of the item.

And in the case of the DiamondInventoryItem, we’ve overwritten the print method. So now let’s go into our index.js file, and let’s see what happens if Alex purchases a Diamond Gold Necklace, and Alex purchases a Diamond Walkman. So we’ll go ahead and save that, and then come out to our terminal and run our application, node index.js. And we see that we cannot afford a Diamond Gold Necklace, and we cannot afford a Diamond Walkman, so I haven’t purchased any items. What we’re going to have to do is give Alex some money so that he can make these purchases.

Let’s give Alex $3000 dollars. So we’ve changed the value that we sent to the Shopper constructor on line eight, and then we’re going to come out here in the terminal and run our application again. And when we do so, we can see that we’re purchasing the Golden Necklace and we cannot afford the Diamond Walkman, because the Diamond Walkman’s very expensive, we actually have to give Alex $4000 dollars to be able to afford that, so I’ll change that value one more time, clear this, and then node our index.js. And there we go, so Alex can finally afford both the Diamond Golden Necklace and the Diamond Walkman.

So you can see that I’ve purchased two items. And, we extended the base inventory item. The base item was called the Necklace. Well, we took the Necklace and made it a Golden Necklace, added 1000 to its cost, and then we took the Necklace and made it a Diamond Golden Necklace and added another $1000 dollars to the cost. And the Diamond Walkman also adds $1000 dollars to the walkman cost, as well as it adds the word Diamond in front of it. Let’s go ahead and print our walkman, and see what we get with that. So I’ve added a call to the print method on the Diamond Walkman, and then when we run that, we see that the Diamond Walkman costs a lot of money.

Leave a Reply

Notify of