Skip to content

Instantly share code, notes, and snippets.

@abdalla
Last active February 3, 2021 13:59
Show Gist options
  • Save abdalla/9b4c26f1b143afc6b8da1067c4a4a781 to your computer and use it in GitHub Desktop.
Save abdalla/9b4c26f1b143afc6b8da1067c4a4a781 to your computer and use it in GitHub Desktop.
Factory Pattern
"use strict";
function Developer(name) {
this.name = name;
this.type = "Developer";
}
function Tester(name) {
this.name = name;
this.type = "Tester";
}
function EnployeeFactory() {
this.create = function (name, type) {
switch (type) {
case 1:
return new Developer(name);
break;
case 2:
return new Tester(name);
break;
}
};
}
function say() {
console.log("Hi, I am " + this.name + " and I am a " + this.type);
}
var eF = new EnployeeFactory();
var employees = [];
employees.push(eF.create("Carlos", 1));
employees.push(eF.create("Abdalla", 2));
employees.forEach(function (x) {
return say.call(x);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment