GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — js_designpatterns/mediator.js at master · m1oto/js_designpatterns

Expand file tree

/

mediator.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
44 lines (41 loc) · 1000 Bytes
/

mediator.js

Copy path

File metadata and controls

44 lines (41 loc) · 1000 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Mediator pattern is used to reduce communication
// complexity between multiple objects or classes.
function Mediator() {
var users = [];
return {
addUser: function(user) {
users.push(user);
},
// Message sending business logic
publishMessage: function(msg, receiver) {
if(receiver) {
receiver.messages.push(msg);
}
else {
users.forEach(function(user) {
user.messages.push(msg);
});
}
}
}
}
// Usage Example
var mediator = Mediator(); // Initialize mediator
// Define user class
function User(name) {
this.name = name;
this.messages = [];
mediator.addUser(this);
}
User.prototype.sendMessage = function(msg, receiver) {
msg = '[' + this.name + ']: ' + msg;
mediator.publishMessage(msg,receiver);
}
// Initialize users
var u1 = new User('Donald');
var u2 = new User('Peter');
var u3 = new User('Anna');
// Message sending
u1.sendMessage('Hi, anybody here?');
u2.sendMessage('Hi Donald, nice to meet you.', u1);
u3.sendMessage('Hi Guys!');
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.