Skip to content

Instantly share code, notes, and snippets.

@csdear
Created January 12, 2015 11:50
Show Gist options
  • Save csdear/95787b4ac251fbe70be9 to your computer and use it in GitHub Desktop.
Save csdear/95787b4ac251fbe70be9 to your computer and use it in GitHub Desktop.
Javascript FACADE PATTERN
/*
○ Building blocks for writing modules.
○ A façade is a high level interface to a larger code base that hides the underlying code complexity.
○ The facade exposes an application programming interface (API) which limits the available functionaity we can use and helps encapsulate a lot of the interior code behavior.
Façade Pattern Structure in JavasScript
• the function returns an object
• the object acts a a façade to the inner implementation , which is encapsulated.
*/
var car = (function () {
//object inner implemenation
var speed = 0;
function driveFaster() {
speed += 10;
}
function printSpeed() {
console.log(speed);
}
return {
// exposed façade API
drive: function(newSpeed) {
while (speed < newSpeed) {
driveFaster();
printSpeed();
}
};
}());
//hitting that exposed façade API function… car.drive() is exposed.
car.drive(80); // output to the console 10, 20, …80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment