forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreational_builder.js
More file actions
75 lines (62 loc) · 1.4 KB
/
creational_builder.js
File metadata and controls
75 lines (62 loc) · 1.4 KB
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
const BUILDER = {
id: 'builder',
name: 'Builder',
type: 'creational',
hint: 'Separates object construction from its representation',
definition: `Separate the construction of a complex object from its representation
so that the same construction process can create different representations.`,
when: 'algorithm of creation is independent of the parts of the object',
codeES5: `function Request() {
this.url = '';
this.method = '';
this.payload = {};
}
function RequestPattern() {
this.request = new Request();
this.forUrl = function(url) {
this.request.url = url;
return this;
};
this.useMethod = function(method) {
this.request.method = method;
return this;
};
this.payload = function(payload) {
this.request.payload = payload;
return this;
};
this.build = function() {
return this.request;
};
}
module.exports = RequestPattern;`,
codeES6: `class Request {
constructor() {
this.url = '';
this.method = '';
this.payload = {};
}
}
class RequestPattern {
constructor() {
this.request = new Request();
}
forUrl(url) {
this.request.url = url;
return this;
}
useMethod(method) {
this.request.method = method;
return this;
}
payload(payload) {
this.request.payload = payload;
return this;
}
build() {
return this.request;
}
}
export default RequestPattern;`
};
export default BUILDER;