Last active
February 3, 2021 13:59
-
-
Save abdalla/9b4c26f1b143afc6b8da1067c4a4a781 to your computer and use it in GitHub Desktop.
Factory Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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