Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console.clear();
- var cl = function(text){ console.log(text); }
- var Participant = function(name){
- this.name = name;
- this.chatroom = null;
- };
- Participant.prototype = {
- send: function(message, to) {
- this.chatroom.send(message, this, to);
- },
- receive: function(message, from) {
- log.add(from.name + ' to ' + this.name + ': ' + message);
- }
- };
- var log = (function(){
- var log = "";
- return {
- add: function(msg) {
- log += msg + "\n";
- },
- show: function() {
- console.log(log);
- log = "";
- }
- };
- })();
- var ChatroomMediator = function() {
- var participants = {};
- return {
- register: function(participant) {
- participants[participant.name] = participant;
- participant.chatroom = this;
- },
- send: function(message, from, to) {
- if (to) {
- to.receive(message, from);
- } else {
- var key;
- for (key in participants) {
- if (participants[key] !== from) {
- participants[key].receive(message, from);
- }
- }
- }
- }
- };
- };
- (function run(){
- var yoko = new Participant('Yoko');
- var john = new Participant('John');
- var paul = new Participant('Paul');
- var chatroom = new ChatroomMediator();
- chatroom.register(yoko);
- chatroom.register(john);
- chatroom.register(paul);
- yoko.send('all you need is love');
- yoko.send('I love you John');
- john.send("Hey, no need to broadcast", yoko);
- paul.send("Ha, I heard that!");
- log.show();
- })();
Advertisement
Add Comment
Please, Sign In to add comment