stuart-d2
1/12/2015 - 11:50 AM

Javascript FACADE PATTERN

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