mediator pattern

javascript design pattern

by kazu69

JavaScript

const Mediator = function(name) {
    this.name = name;
    this.message = '';
}

Mediator.prototype = {
    say: function(message) {
        this.message = message;
    },
    send: function() {
        console.log(`${this.name}: ${this.message}`);
    }
}

const Tom = new Mediator('Tom');
const Bob = new Mediator('Bob');
Tom.say('Hello');
Tom.send();
Bob.say('Hi');
Bob.send();