Coding Guru

Code never does the mistake, we make the mistakes in the code. Think, Imagine, Phrase and then Code.

The Facade Pattern

The Facade pattern provides a unified interface over a set of interfaces in the subsystem. Facade means a higher-level interface which makes the subsystem easy and efficient to use.

The core purpose of this pattern is to conceal the complexity of code by using an anonymous method as an extra layer involved. Internal subroutines are never being exposed however invoked through the facade which makes the pattern secure such that it never exposes anything to the user/developers working with it.The pattern is both interesting and useful for adding an layer of security to existing minified code. This pattern is useful when coupled in the revealing module pattern.

On the server side, in a multi-layer application architecture we have a presentation layer which acts as a client to the service layer. The communication between these layers takes place through a well-defined API. This API or layer, hides the complexities  in the business objects from the presentation layer.

Diagram


Sample code

 In the following sample code the Mortgage object is the Facade. It gives a simple interface to the client with only one method: applyFor. Eut underneath this simple API lies considerable complexity.

The applicant's name is passed into the Mortgage constructor function. Then the applyFor method is called with the requested loan amount. Internally, this method uses services from 3 separate subsystems that are complex and possibly take some time to process; they are Bank, Credit, and Background.

Based on several criteria (bank statements, credit reports, and criminal background) the applicant is either accepted or denied for the requested loan.

var Mortgage = function (name) {
this.name = name;
}

Mortgage.prototype = {

applyFor: function (amount) {
// access multiple subsystems...
var result = "approved";
if (!new Bank().verify(this.name, amount)) {
result = "denied";
} else if (!new Credit().get(this.name)) {
result = "denied";
} else if (!new Background().check(this.name)) {
result = "denied";
}
return this.name + " has been " + result +
" for a " + amount + " mortgage";
}
}

var Bank = function () {
this.verify = function (name, amount) {
// complex logic ...
return true;
}
}

var Credit = function () {
this.get = function (name) {
// complex logic ...
return true;
}
}

var Background = function () {
this.check = function (name) {
// complex logic ...
return true;
}
}

function run() {
var mortgage = new Mortgage("Joan Templeton");
var result = mortgage.applyFor("$100,000");

alert(result);
}

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).

Loading