JavaScript: Factory Pattern
Blogs20132013-09-17
JavaScript: Factory Pattern
The following is a typical Factory pattern for JavaScript, original from Stoyan Stefanov.
//1. constructor:
function CarMaker() {}
//2. Factory Items Iteration:
CarMaker.Compact = function(){};
CarMaker.Convertible = function(){};
CarMaker.SUV = function() {};
//3. public methods:
CarMaker.prototype.tool = function() {};
CarMaker.prototype.drive = function() {};
//4. Static methods:
CarMaker.factory = function(type) {
var constr = type;
if(typeof CarMaker[constr] !== 'function') {
throw {
name: "Error",
message: constr + 'not exist'
};
}
//inheritance only once
if(typeof CarMaker[constr].prototype.tool !== 'function') {
CarMaker[constr].prototype = new CarMaker();
}
return new CarMaker[constr]();
}
//5. Usage:
var camry = CarMaker.factory('Convertible');
var cherokee = CarMaker.factory('SUV');
camry.tool();
cherokee.drive(); 