-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory-pattern.js
66 lines (51 loc) · 1.23 KB
/
factory-pattern.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// factory pattern is a creational design pattern that uses factory methods to create objects
// factory functions simply creates an object and returns it.
class Devloper {
name = "";
type = "Devloper"
constructor(name) {
this.name = name;
}
say() {
console.log(`Hi i'm ${this.name} & i'm ${this.type}`)
}
}
class Tester {
name = "";
type = "Tester"
constructor(name) {
this.name = name;
}
say() {
console.log(`Hi i'm ${this.name} & i'm ${this.type}`)
}
}
class Analyst {
name = "";
type = "Analyst"
constructor(name) {
this.name = name;
}
say() {
console.log(`Hi i'm ${this.name} & i'm ${this.type}`)
}
}
class EmployeeFactory {
create(name, type) {
switch (type) {
case 1:
return new Devloper(name);
case 2:
return new Tester(name);
case 3:
return new Analyst(name);
default:
break;
}
}
}
const employeeFactory = new EmployeeFactory();
const employees = [];
employees.push(employeeFactory.create("Ajith", 1));
employees.push(employeeFactory.create("Vijay", 2));
console.log(employees)