Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We add a class GoldenInventoryItem, and a class DiamondInventoryItem. So for GoldenInventoryItem, we have to 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 we take the baseItem name, and we extend it by adding the word Golden in front of it. We also take the price of the item, so whatever the baseItem price is, we just add 1000 to that. So we are extending the baseItem. We do the same thing with the DiamondInventoryItem. But 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 do something a little different, we 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.
- Loading branch information