-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryPattern.ts
More file actions
107 lines (87 loc) · 2.96 KB
/
Copy pathFactoryPattern.ts
File metadata and controls
107 lines (87 loc) · 2.96 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass,
but allows subclasses to alter the type of objects that will be created.
*/
/*
On this pattern, there are some considerations:
1. An interface that is common to all objects that can be created by the creator and its subclases.
2. Different implementations of the interface.
3. The Creator class declares the factory method that returns the objects considered on the interface.
The factory method returns the interface type object.
4. We can use abstract method in the factory method and redefine ton create different types of objects.
*/
interface Engine {
start(): string;
}
class ElectricEngine implements Engine {
start(): string {
return "Starting electric engine";
}
}
class DieselEngine implements Engine {
start(): string {
return "Starting diesel engine";
}
}
class JetEngine implements Engine {
start(): string {
return "Starting jet engine";
}
}
class EngineFactory {
static createEngine(type: string): Engine {
switch (type.toLowerCase()) {
case "electric":
return new ElectricEngine();
case "diesel":
return new DieselEngine();
case "jet":
return new JetEngine();
default:
throw new Error(`Engine not supported.`);
}
}
}
const electric = EngineFactory.createEngine("electric");
console.log(electric.start());
const diesel = EngineFactory.createEngine("diesel");
console.log(diesel.start());
const jet = EngineFactory.createEngine("jet");
console.log(jet.start());
/*
// With abstract class
abstract class EngineFactory {
public abstract createEngine(): Engine;
}
class ElectricEngineFactory extends EngineFactory {
createEngine() {
return new ElectricEngine();
}
}
class DieselEngineFactory extends EngineFactory {
createEngine() {
return new DieselEngine();
}
}
class JetEngineFactory extends EngineFactory {
createEngine() {
return new JetEngine();
}
}
const ElectricFactory = new ElectricEngineFactory();
const electric = ElectricFactory.createEngine();
console.log(electric.start());
const DieselFactory = new DieselEngineFactory();
const diesel = DieselFactory.createEngine();
console.log(diesel.start());
const JetFactory = new JetEngineFactory();
const jet = JetFactory.createEngine();
console.log(jet.start());
*/
/*
Recomendations of usage
- When you don't know beforehand the exacct types and dependencies of the objects will be created in the code.
- It's way to extend the internal components of a library or framework.
- When it's a good option to save system resources by reusing existing objects instead of rebuild tem each time.
*/
// THE CODE IS EXECUTED WITH THE NEXT COMMAND: npx tsx FactoryPattern.ts