-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1-theory.js
More file actions
90 lines (71 loc) · 1.51 KB
/
1-theory.js
File metadata and controls
90 lines (71 loc) · 1.51 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
class Product {
constructor() {
this.field1 = 'value1';
this.field2 = 'value2';
this.field3 = 'value3';
}
}
class AbstractBuilder {
constructor() {
const proto = Object.getPrototypeOf(this);
if (proto.constructor === AbstractBuilder) {
throw new Error('Abstract class should not be instanciated');
}
}
createInstance() {
throw new Error('Method is not implemented');
}
step1() {
throw new Error('Method is not implemented');
}
step2() {
throw new Error('Method is not implemented');
}
step3() {
throw new Error('Method is not implemented');
}
getInstance() {
throw new Error('Method is not implemented');
}
}
class ConcreteBuilder extends AbstractBuilder {
constructor() {
super();
this.instance = null;
}
createInstance() {
this.instance = new Product();
}
step1() {
this.instance.field1 = 'step1';
}
step2() {
this.instance.field2 = 'step2';
}
step3() {
this.instance.field3 = 'step3';
}
getInstance() {
return this.instance;
}
}
class Director {
constructor(builder) {
this.builder = builder;
}
createInstance() {
this.builder.createInstance();
this.builder.step1();
this.builder.step2();
this.builder.step3();
return this.builder.getInstance();
}
}
// Usage
const builder = new ConcreteBuilder();
console.dir(builder);
const director = new Director(builder);
console.dir(director);
const instance = director.createInstance();
console.dir(instance);