Skip to content

Instantly share code, notes, and snippets.

@UlisesGascon
Created June 13, 2015 08:29
Show Gist options
  • Save UlisesGascon/c0ea489a601b3fe37769 to your computer and use it in GitHub Desktop.
Save UlisesGascon/c0ea489a601b3fe37769 to your computer and use it in GitHub Desktop.
function FactoriaVehiculos() {
this.createVehicle = function (type) {
var vehicle;
if (type === "coche") {
vehicle = new Coche();
} else if (type === "moto") {
vehicle = new Moto();
} else if (type === "autobus") {
vehicle = new Autobus();
} else if (type === "sidecar") {
vehicle = new Sidecar();
}
vehicle.type = type;
vehicle.say = function () {
log.add(this.type + " tiene " + this.wheel + " Ruedas");
}
return vehicle;
}
}
var Coche = function () {
this.wheel = 4;
};
var Moto = function () {
this.wheel = 2;
};
var Autobus = function () {
this.wheel = 8;
};
var Sidecar = function () {
this.wheel = 3;
};
// log helper
var log = (function () {
var log = "";
return {
add: function (msg) { log += msg + "\n"; },
show: function () { alert(log); log = ""; }
}
})();
function run() {
var vehicles = [];
var factory = new FactoriaVehiculos();
vehicles.push(factory.createVehicle("moto"));
vehicles.push(factory.createVehicle("coche"));
vehicles.push(factory.createVehicle("autobus"));
vehicles.push(factory.createVehicle("sidecar"));
for (var i = 0, len = vehicles.length; i < len; i++) {
vehicles[i].say();
}
log.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment