Abstract Factory Pattern
Encapsulate a group of individual factories with a common goal, separating the details of implementation of a set of objects from their general usage.
by Steven Senkus
JavaScript
var AbstractVehicleFactory = (function () {
var types = {};
return {
getVehicle: function (type, customizations) {
var Vehicle = types[type];
return (Vehicle ? new Vehicle(customizations) : null);
},
registerVehicle: function (type, Vehicle) {
var proto = Vehicle.prototype;
if (proto.drive && proto.breakDown) {
types[type] = Vehicle;
}
return AbstractVehicleFactory;
}
};
}());
console.log(AbstractVehicleFactory.getVehicle);
var car = AbstractVehicleFactory.getVehicle("car", {
color: 'lime green',
state: 'like new'
});