Skip to content

Instantly share code, notes, and snippets.

@tdimnet
Last active September 20, 2021 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdimnet/86e49dedfa143beedd3316a496048ff0 to your computer and use it in GitHub Desktop.
Save tdimnet/86e49dedfa143beedd3316a496048ff0 to your computer and use it in GitHub Desktop.
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