Skip to content

Instantly share code, notes, and snippets.

@wolivera
Created June 23, 2022 12:42
Show Gist options
  • Save wolivera/7ff3041edaf47d5a68191a9c02806bd7 to your computer and use it in GitHub Desktop.
Save wolivera/7ff3041edaf47d5a68191a9c02806bd7 to your computer and use it in GitHub Desktop.
AbstractFactory
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