Skip to main content. Skip to secondary content (sidebar).

The facade design pattern in JavaScript

I started to think about design patterns recently while working on a big project. I realized I could probably benefit from one or more of these patterns as the complexity of the client-side portion of my application began to grow. After a little investigation I stumbled across the facade design pattern. This pattern promotes readability by grouping common tasks, and it can simplify a complex API by providing a single point of access. Especially with JavaScript it can help to reduce the size of the code base.

Accessing objects directly

When an event occurs in a Web application, like clicking a button, several actions may need to happen in succession. Usually these actions involve gaining direct access to a few different objects and their methods and properties, and depending on the event, the behavior of those objects may need to change. If the objects exist within an API, then that means every developer needs to learn the nuances of each object in order to extend the application. The following diagram helps to illustrate the point.

Accessing objects through the facade

In the previous example there is only one level of objects, and two events that use them, but in reality there might be dozens of objects and numerous events. If the events share some common functionality, then it makes sense to group that into a single implementation. The grouping is what is known as your facade. The facade sits between your events and objects, and it masks the details of the operation, distributing responsibilities to the objects appropriately.

In more complex examples it even returns values back through to the event call, as well as taking in arguments to use as method parameters. Only the architect who creates the facade will need to know the underlying objects that are utilized, while other programmers are free to use the simplified “interface”. The following diagram helps to illustrate the point.

Creating the facade

Creating a facade is fairly straightforward, and it is a great pattern to implement when refactoring code in order to gain a boost in performance. Remember, if you can find common tasks that are shared between events, then the facade is a great place to start. One of the better code abstracts I have read (written in Java), was from Wikipedia. It helps to visualize the concept in another language first before giving it a try in JavaScript.

The following example is an abstract that also uses the module pattern. The objects are labeled explicitly so that you can better spot the facade.

var DBUG = function() {
	_private = {
		getThis : function( count ) {
			/* ... implementation ... */
		},
		putThis : function() {
			/* ... implementation ... */
		},
		runThis : function() {
			/* ... implementation ... */
		}
	};
	return {
		facade : function( args ) {
			_private.getThis( args.count );
			_private.putThis();
			if ( args.runIt ) {
				_private.runThis();
			}
		}
	}
}();

window.onload = function() {
	DBUG.facade({
		runIt : true,
		count : 1
	});
	DBUG.facade({
		runIt : false,
		count : 2
	});
}

As I mentioned, this is really an abstract, but I think it aptly demonstrates the benefits of using a facade with JavaScript. I only used the window.onload event as an example, and you would likely have several events calling the same facade. In this example you can pass in arguments, which can be used for conditions that delegate specific behavior.

As you build any Web application that continues to grow, and objects are added to the namespace, think about how the facade design pattern can help you simplify the implementation. If you want to read more about patterns, check out Pro JavaScript Design Patterns, Head First Design Patterns and Design Patterns: Elements of Reusable Object-Oriented Software.

A day late and a dollar short... comments are closed.

01  |  December 25th, 2008 at 3:34 pm

The value to me in this post was the two diagrams to show what the facade did. The code example was a little abstract but motivated me to look at the books.

Interesting that you use HeadFirst. I like that imprint. I also like APress, but those two imprints tend to be for different audiences.

I’m actually having a problem with a small coding project that I think facade could help. I may get there, because I’d like to make the process behind this particular effort more repeatable.

Comment by:

Bud Gibson

02  |  December 26th, 2008 at 3:27 pm

Hi Bud,

I’m glad the diagrams helped out some. This is one of those patterns that is hard to replicate in the abstract. I appreciate what you’ve been doing with MichiganInnovators.org, and if you need any recommendations on how to proceed with some JavaScript let me know — I would be happy to help out pro bono.

Comment by:

Brian

03  |  December 28th, 2008 at 9:02 pm

Hi Brian, thanks for the response. I just popped you an email, using the address listed here, about getting together.

Comment by:

Bud Gibson

04  |  December 30th, 2008 at 8:59 am

We’re using the facade pattern where I work in Java land.
The theory, the concept is great. Hide complex multi-point talking into a niec ‘facade’ method that exposes a nice clean singular method.
It in theory provides you the ability in the future to change a side of the facade without having to change the other side.

In practice though I think it’s far more trouble than it’s worth.
You spend a lot of time writing these things, and a lot of code goes into them.
Less of a problem under JavaScript, but under Java a great deal of time is just spend converting from one data format to another.

If I’m ever involved in another project in the future where the facade pattern is brought up as a way to solve XYZ, I’m going to strongly suggest that we all take a step back and examine why they feel that’s a good approach and what might work better.

Comment by:

Robert Schultz

05  |  January 25th, 2009 at 9:14 am

[...] The facade design pattern in JavaScript [...]

Comment by:

Links for the day - 25-01-2009 | Blogmines