function Developer(name) {
this.name = name;
}
function Tester(name) {
this.name = name;
}
function CreateUser() {
this.create = function (type, name) {
switch (type.name) {
case Developer.name:
return new Developer(name);
case Tester.name:
return new Tester(name);
default:
return new Tester(name);
}
};
}
const developer = new CreateUser().create(Developer, "dev101");
const tester = new CreateUser().create(Tester, "tester101");
console.log(developer);
console.log(tester);
Factory Pattern
Using ES5 syntax
class Developer {
constructor(name) {
this.name = name;
}
}
class Tester {
constructor(name) {
this.name = name;
}
}
class CreateUser {
constructor() {}
create(type, name) {
switch (type.name) {
case Developer.name:
return new Developer(name);
case Tester.name:
return new Tester(name);
default:
return new Tester(name);
}
}
}
const developer = new CreateUser().create(Developer, "dev101");
const tester = new CreateUser().create(Tester, "tester101");
console.log(developer);
console.log(tester);