-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflyweight.js
More file actions
48 lines (40 loc) · 732 Bytes
/
flyweight.js
File metadata and controls
48 lines (40 loc) · 732 Bytes
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
//Flyweight
class Auto {
constructor(model) {
this.model = model
}
}
class AutoFactory {
constructor(name) {
this.models = {}
}
create(name) {
let model = this.models[name]
if (model) return model
console.count('model')
this.models[name] = new Auto(name)
return this.models[name]
}
getModels() {
console.log(this.models)
console.table(this.models)
}
}
const factory = new AutoFactory()
const bmw = factory.create('BMW')
const audi = factory.create('Audi')
const tesla = factory.create('Tesla')
const blackTesla = factory.create('Tesla')
/*
model: 1
model: 2
model: 3
*/
factory.getModels()
/*
{
"BMW": {"model": "BMW" },
"Audi": {"model": "Audi" },
"Tesla": { "model": "Tesla" }
}
*/