Created
July 12, 2022 13:12
-
-
Save wolivera/f0ac2598418f21c0d0660042fa7df662 to your computer and use it in GitHub Desktop.
GoF Mediator
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 Bidder { | |
constructor (name) { | |
this.name = name; | |
this.auction = null; | |
} | |
bid (amount, to) { | |
this.auction.send(amount, this, to); | |
} | |
offer (amount, from) { | |
console.log(from.name + " offered " + amount); | |
} | |
} | |
class Auctioneer { | |
constructor () { | |
this.participants = {}; | |
} | |
register (participant) { | |
this.participants[participant.name] = participant; | |
participant.auction = this; | |
} | |
send (amount, from, to) { // broadcast message | |
for (let key in this.participants) { | |
if (this.participants[key] !== from) { | |
this.participants[key].offer(amount, from); | |
} | |
} | |
} | |
}; | |
const julie = new Bidder("Julie"); | |
const annie = new Bidder("Annie"); | |
const paul = new Bidder("Paul"); | |
const auction = new Auctioneer(); | |
auction.register(julie); | |
auction.register(annie); | |
auction.register(paul); | |
julie.bid(20); | |
julie.bid(10); | |
// Julie offered 20 | |
// Julie offered 20 | |
// Julie offered 10 | |
// Julie offered 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment