w11 design patterns
This commit is contained in:
44
w10-patterns/observer/index.js
Normal file
44
w10-patterns/observer/index.js
Normal file
@@ -0,0 +1,44 @@
|
||||
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
|
||||
Reference in New Issue
Block a user