Created
June 23, 2022 12:42
-
-
Save wolivera/7ff3041edaf47d5a68191a9c02806bd7 to your computer and use it in GitHub Desktop.
AbstractFactory
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
class Employee { | |
constructor(name) { | |
this.name = name; | |
} | |
say() { | |
console.log("I am employee " + this.name); | |
}; | |
} | |
class EmployeeFactory { | |
create(name) { | |
return new Employee(name); | |
}; | |
} | |
class Vendor { | |
constructor(name) { | |
this.name = name; | |
} | |
say() { | |
console.log("I am vendor " + this.name); | |
}; | |
} | |
class VendorFactory { | |
create(name) { | |
return new Vendor(name); | |
}; | |
} | |
const persons = []; | |
const employeeFactory = new EmployeeFactory(); | |
const vendorFactory = new VendorFactory(); | |
persons.push(employeeFactory.create("Joao")); | |
persons.push(employeeFactory.create("Tim")); | |
persons.push(vendorFactory.create("Nicole")); | |
persons.push(vendorFactory.create("Pedro")); | |
for (let i = 0, len = persons.length; i < len; i++) { | |
persons[i].say(); | |
} | |
// I am employee Joao | |
// I am employee Tim | |
// I am vendor Nicole | |
// I am vendor Pedro |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment