Decorator Pattern in JavaScript
Last time I mentioned I was going to focus on some OO-related javascript concerns. For this post, I want to talk about a programming pattern that I use quite often: the Decorator pattern.
The Decorator pattern is defined as “a design pattern that allows behavior to be added to an individual object, either statically or dynammically, without affecting the behavior of other objects from the same class.” (source)
What this means in plain english is that we can extend an object’s functionality— even at runtime—and even with multiple decorators. This is pretty common in the ‘classical’ OO world of, say, java. Here’s how we’d do it prototypically in javascript.
Let’s say that we’ve got a Guitar store shopping cart written in javascript. At checkout time the user can select various options or upgrades to add to their guitar. We will implement these options as decorators.
First, the guitar model. It will manage a list of decorators. Here’s the constructor:
1 2 3 4 |
|
Next, the decorators themselves. Decorators will be implemented as properties of
Guitar.decorators
.
1
|
|
Now, we add the various decorator properties. These are the options/upgrades the user can select.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Here’s the Guitar’s decorate method. This is the method called at runtime, and
it simply pushes each decorator onto the guitar’s decoratorList
.
1 2 3 4 |
|
Notice that we return the guitar instance. This is so we can chain decorator calls.
Finally, our getPrice()
method for our shopping cart checkout. It simply goes
through a list of decorators and calls each decorator’s getPrice()
method, passing
the results from the previous call.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
That’s all there is to it. Here’s the client code in action. We’ll create a couple of guitars and print out the price.
1 2 3 4 5 6 7 |
|
This ought to whet your appetite for finding other uses for the decorator pattern in your code.