In my last blog post article I discussed the Singleton Design Pattern and how it is used in JavaScript. This time around the Bridge design pattern is poking its head up and asking for a bit of attention, making it the second in the JavaScript Design Patterns series.
Every place I’ve read about the Bridge pattern likes to quote the Gang of Four directly in their description of what the Bridge pattern is, so I figure why not me too? The Bridge pattern should “decouple an abstraction from its implementation so that the two can vary independently.” Bridges are quite beneficial in event-driven applications, which are quite common in JavaScript. Considering that fact, it’s surprisingly one of the most underused design patterns.
Event Listeners Example
In the following examples, I’ll be using a bit of jQuery, so if you don’t know what a function does or how it works, you can look at their documentation.
Below you’ll see a small bit of code using an API method called getXById
, which is badly implemented. We are using a click event to determine which element to pull the id from. The callback itself is getXById
, which gets the id from the clicked element and uses AJAX to get X from the server using the Id it found.
1 | getXById = function() { |
This code isn’t all that bad if it’s only meant to be used that one specific way on the one specific page, but it’s (supposedly) part of an API, so this needs to be changed a lot. Let’s decouple getXById
from the event listener and the implementation of what is done with the result:
1 | getXById = function(id, callback) { |
Now the getXById
can be used just about anywhere and you can do anything with X now.
Classical Example
What I mean by “classical” is two-fold: the example is more common to classic object-oriented programming languages and it is uses classes. Technically JavaScript doesn’t have classes but you mimic interfaces and use prototypes to mimic classes. Originally this example was in the book Head First Design Patterns, which uses Java in their example. However, this is one the minor patterns in the back of the book that didn’t actually have a code example, so I’m really just using the diagrams (except I recreated them because I’m awesome).
Our Starting Product
We start with the RemoteControl
Interface. ToshibaRemote
and SonyRemote
both implement that interface to work with their respective televisions. With this code, you can call on()
, off()
, or setChannel()
on any remote and even though all the TVs are different, it’ll work. What happens though, when you want to make improvements on the remotes? That’s where the Bridge pattern comes in:
Now, since the TVs adhere to an interface and all of the remotes adhere to another interface – actually just a class because it only needs the one implementation – we can create variations to either on through inheritance and still be compatible. Wanna see some code? I’ll show you the code for the new solution with the Bridge pattern, but I don’t think you need to see the code for the original. I really don’t think many of you need to see any code at all, but I’m sure there are those out there who’d like to see it anyway. We’re programmers, right? Show us the code!
1 | var RemoteControl = function(tv) { |
Well, that just about wraps things up for the Bridge pattern in JavaScript. If you haven’t yet, make sure you go back and read about the Singleton Pattern. Also keep an eye out for the next article in this series: the Composite Pattern. If you thought this was helpful or you just plain liked the article, please spread the word using the social sharing buttons below the post. Thanks!
JavaScript Design Patterns series: