Last active
September 20, 2021 12:52
-
-
Save tdimnet/86e49dedfa143beedd3316a496048ff0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Subject { | |
constructor() { | |
this._observers = [] | |
} | |
subscribe(observer) { | |
this._observers.push(observer) | |
} | |
unsubscribe(observer) { | |
this._observers = this._observers.filter(obs => obs !== observer) | |
} | |
fire(productName) { | |
this._observers.forEach(observer => observer.addToWishList(productName)) | |
} | |
} | |
class WishList { | |
constructor() { | |
this.products = [] | |
} | |
addToWishList(productName) { | |
console.log(`Add the product ${productName} to the wishlist`) | |
this.products.push(productName) | |
} | |
} | |
class Mailer { | |
constructor(email) { | |
this._email = email | |
} | |
addToWishList(productName) { | |
console.log(`Send an email to ${this._email} with the product name ${productName}`) | |
} | |
} | |
const Sub = new Subject() | |
const Mail = new Mailer('email@example.com') | |
const WL = new WishList() | |
Sub.subscribe(Mail) | |
Sub.subscribe(WL) | |
Sub.fire('Terminator') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment