exercises/w11-patterns/w8_observer/index.js
2025-05-06 14:20:09 +09:00

45 lines
724 B
JavaScript

import { Observer, Subject } from './Subject.js';
class Fish {
constructor(weight) {
super();
this.weight = weight;
}
getWeight() {
return this.weight;
}
getSpecies() {
return this.species;
}
}
class Salmon extends Fish {
constructor() {
super(2);
this.species = 'Atalntinc salmon';
}
}
class Tuna extends Fish {
constructor() {
super(200);
this.species = 'Bluefin tuna';
}
}
class Fisherman {
constructor(name) {
super();
this.name = name;
}
}
// Main
const bob = new Fisherman('Bob');
const fish1 = new Tuna();
const fish2 = new Salmon();
// 1. subsribe bob (observer) to fish1 and fish2 (subjects)
// 2. fishes (subjects) notify when they are caught