-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBridge.js
More file actions
47 lines (37 loc) 路 1.07 KB
/
Copy pathBridge.js
File metadata and controls
47 lines (37 loc) 路 1.07 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
// Implement breathing behavior for different living beings like fish, humans, and trees, which share a common interface but have varying implementations.
class Breathing {
breathe() {
throw new Error("Must implement breathe()");
}
}
class Gills extends Breathing {
breathe() {
console.log("馃悷 Breathing through gills (underwater)");
}
}
class Lungs extends Breathing {
breathe() {
console.log("馃懁 Breathing through lungs (air)");
}
}
class Stomata extends Breathing {
breathe() {
console.log("馃尦 Breathing through stomata (leaves)");
}
}
class LivingThing {
constructor(name, breathingType) {
this.name = name;
this.breathingType = breathingType;
}
breathe() {
console.log(`${this.name} is breathing:`);
this.breathingType.breathe();
}
}
const fish = new LivingThing("Fish", new Gills());
const human = new LivingThing("Human", new Lungs());
const tree = new LivingThing("Tree", new Stomata());
fish.breathe();
human.breathe();
tree.breathe();