-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarFactoryPattern.ts
More file actions
56 lines (43 loc) · 1.02 KB
/
CarFactoryPattern.ts
File metadata and controls
56 lines (43 loc) · 1.02 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
// factory DP
class Car{
private make: string;
private model: string;
private numDoor: number;
private color: string;
constructor(make: string, model: string){
this.model = model;
this.make = make;
this.numDoor = 0;
this.color = "";
}
setNUmDoors(doors: number){
this.numDoor = doors
}
setColor(color: string){
this.color= color;
}
}
class CarFactory{
private car: Car;
private makeCar: ()=> Car;
constructor(makeCar: ()=>Car){
this.makeCar = makeCar;
}
build(doors: number, color: string){
this.car = this.makeCar();
this.car.setNUmDoors(doors);
this.car.setColor(color);
}
getCar(): Car{
return this.car;
}
}
function makeFord(): Car{
return new Car("Ford", "Pinto")
}
function makeHonda(): Car{
return new Car("Honda", "Civic")
}
let fordFactory = new CarFactory(makeFord);
fordFactory.build(4,"green");
console.log(fordFactory.getCar());