-
Notifications
You must be signed in to change notification settings - Fork 2
/
AbstractFactory.ts
66 lines (56 loc) · 1.52 KB
/
AbstractFactory.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
54
55
56
57
58
59
60
61
62
63
64
65
66
abstract class AbstractProductA {
abstract methodA(): void;
abstract methodB(): void;
}
abstract class AbstractProductB {
abstract methodA(): void;
abstract methodB(): void;
}
class ProductA extends AbstractProductA {
constructor(value: String) {
super();
console.log(value);
}
methodA(): void {}
methodB(): void {}
}
class ProductB extends AbstractProductB {
constructor(value: String) {
super();
console.log(value);
}
methodA(): void {}
methodB(): void {}
}
abstract class AbstractFactory {
abstract createProductA(): AbstractProductA;
abstract createProductB(): AbstractProductA;
}
class NewYorkFactory extends AbstractFactory {
createProductA(): ProductA {
return new ProductA('ProductA made in New York');
}
createProductB(): ProductB {
return new ProductB('ProductB made in New York');
}
}
class CaliforniaFactory extends AbstractFactory {
createProductA(): ProductA {
return new ProductA('ProductA made in California');
}
createProductB(): ProductB {
return new ProductB('ProductB made in California');
}
}
// USAGE:
const nyFactory = new NewYorkFactory();
nyFactory.createProductA();
nyFactory.createProductB();
const calFactory = new CaliforniaFactory();
calFactory.createProductA();
calFactory.createProductB();
// OUTPUT:
// "ProductA made in New York"
// "ProductB made in New York"
// "ProductA made in California"
// "ProductB made in California"