-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.ts
53 lines (44 loc) · 1.23 KB
/
builder.ts
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
/*
Provide methods for building an object, rather than muitiple complex constructors
Allows building complex objects in simpler steps
*/
module Builder {
export class CarBuilder {
name: string;
year: number;
make: string;
model: string;
constructor(name: string) {
this.name = name;
}
setYear(value: number): CarBuilder {
this.year = value;
return this;
}
setModel(make: string, model: string): CarBuilder {
this.make = make;
this.model = model;
return this;
}
build(): Car {
return new Car(this);
}
}
export class Car {
private name: string;
private year: number;
private make: string;
private model: string;
constructor(builder: CarBuilder) {
this.name = builder.name;
this.year = builder.year;
this.make = builder.make;
this.model = builder.model;
}
}
}
var myOldCar = new Builder.CarBuilder("Rusty")
.setModel("Skoda", "Fabia")
.setYear(2000)
.build();
write(myOldCar);