-
Notifications
You must be signed in to change notification settings - Fork 1
/
12-10-createBird.js
116 lines (102 loc) Β· 2.3 KB
/
12-10-createBird.js
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
108
109
110
111
112
113
114
115
116
/**
* p518 μμ
*/
function createBird(data) {
return new Bird(data);
}
class Bird {
#name;
constructor(data) {
this.#name = data.name;
this._plumage = data.plumage;
this._speciesDelegate = this.selectSpeciesDelegate(data);
}
selectSpeciesDelegate(data) {
switch (data.type) {
case "μ λ½ μ λΉ":
return new EuropeanSwallowDelegate(data, this);
case "μν리카 μ λΉ":
return new AfricanSwallowDelegate(data, this);
case "λ
Έλ₯΄μ¨μ΄ νλ μ΅λ¬΄":
return new NorwegianBlueParrotDelegate(data, this);
default:
return new SpeciesDelegate(data, this);
}
}
get name() {
return this.#name;
}
get plumage() {
return this._speciesDelegate.plumage;
}
get airSpeedVelocity() {
return this._speciesDelegate.airSpeedVelocity;
}
}
class SpeciesDelegate {
constructor(data, bird) {
this._bird = bird;
}
get plumage() {
return this._bird._plumage || "보ν΅μ΄λ€";
}
get airSpeedVelocity() {
return null;
}
}
class EuropeanSwallowDelegate extends SpeciesDelegate {
get airSpeedVelocity() {
return 35;
}
}
class AfricanSwallowDelegate extends SpeciesDelegate {
#numberOfCoconuts;
constructor(data, bird) {
super(data, bird);
this.#numberOfCoconuts = data.numberOfCoconuts;
}
get airSpeedVelocity() {
return 40 - 2 * this.#numberOfCoconuts;
}
}
class NorwegianBlueParrotDelegate extends SpeciesDelegate {
#voltage;
#isNailed;
constructor(data, bird) {
super(data, bird);
this.#voltage = data.voltage;
this.#isNailed = data.isNailed;
}
get plumage() {
if (this.#voltage > 100) return "κ·Έμλ Έλ€";
else return this._bird._plumage || "μμλ€";
}
get airSpeedVelocity() {
return this.#isNailed ? 0 : 10 + this.#voltage / 10;
}
}
/**
* μμ μ€νμ μν μμμ μ½λ
*/
const data = [
{
name: "μ",
},
{
name: "μ λ½ μ λΉ",
type: "μ λ½ μ λΉ",
},
{
name: "μν리카 μ λΉ",
type: "μν리카 μ λΉ",
numberOfCoconuts: 2,
},
{
name: "λ
Έλ₯΄μ¨μ΄ νλ μ΅λ¬΄",
type: "λ
Έλ₯΄μ¨μ΄ νλ μ΅λ¬΄",
voltage: 0,
isNailed: false,
},
];
const birds = data.map((d) => createBird(d));
birds.forEach((b) => console.log(b.name, b.plumage, b.airSpeedVelocity));