-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfactory.js
97 lines (71 loc) · 1.45 KB
/
factory.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Ejemplo Fabrica de personas
*/
class person{
constructor(name,surname,age,sex){
this.name = name;
this.surname = surname;
this.age = age;
this.sex = sex;
}
}
class personFactory{
constructor(name,surname,age,sex){
this.name = null;
this.surname = null;
this.age = null;
this.sex = null;
}
setName(name){
this.name = name;
}
setSurname(surname){
this.surname = surname;
}
setAge(age){
this.age = age;
}
setSex(sex){
this.sex = sex;
}
build(){
return new person(this.name,this.surname,this.age,this.sex);
}
}
//Invocación.
let fabrica = new personFactory();
fabrica.setName('Damián');
fabrica.setSurname('Cipolat');
fabrica.setAge(30);
fabrica.setSex('M');
let damian = fabrica.build();
console.log(damian);
/*
Ejemplo fabrica de profilers.
*/
class profiler{
constructor(label){
this.label = label;
this.lastTime = null;
}
start(){
this.lastTime = process.hrtime();
}
end(){
let dif = process.hrtime(this.lastTime);
console.log('Timer "' + this.label + '" took '+ diff[0] + ' seconds and '+ diff[1] + ' nanoseconds.');
}
}
//Invocación.
if(process.env.NODE_ENV === 'development')
return new Profiler(label);
else{
if(process.env.NODE_ENV === 'production')
return {
start: function() {},
end: function() {}
}
else{
throw new Error('Must set NODE_ENV');
}
}