Carl Danley

The Facade Pattern

Posted by Carl Danley on .
Featured

The Facade Pattern

Posted by Carl Danley on .

The purpose of the facade pattern is to conceal the underlying complexity of the code by using an anonymous function as an extra layer. Internal subroutines are never exposed but rather invoked through a facade which makes this pattern secure in that it never exposes anything to the developers working with it. The facade pattern is both extremely interesting and very useful for adding an extra layer of security to your already minified code. This pattern is extremely useful when coupled with the revealing module pattern.

This article is part of a series called JavaScript Design Patterns.

Advantages

  • Enhances security for your web application
  • Works well in combination with other patterns
  • Easy to implement
  • Makes it easy to patch internals
  • Provides a simpler public interface
  • Proven useful for other major libraries such as jQuery

Disadvantages

There aren't any real drawbacks as it provides a unified interface to a set of interfaces in a subsystem. As a result, you aren't forced to make any unwanted compromises, thus a win-win. One possible note worth mentioning is that a developer must decide whether the implicit cost of implementation is really worth the abstraction (though this is generally a small footprint).

Examples

The above is a basic example of the facade pattern.

The above is a more advanced version of the facade pattern that adds security to internal methods.

Additional Resources

Other Common JS Patterns