-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathMediator.js
53 lines (44 loc) · 989 Bytes
/
Mediator.js
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
45
46
47
48
49
50
51
52
53
/*
Mediator Design Pattern -> https://youtu.be/ZuhgOu-DGA4
Author: DevSage (Youtube) -> https://www.youtube.com/DevSage
*/
function Member(name)
{
this.name = name
this.chatroom = null
}
Member.prototype = {
send: function(message, toMember)
{
this.chatroom.send(message, this, toMember)
},
receive: function(message, fromMember)
{
console.log(`${fromMember.name} to ${this.name}: ${message}`)
}
}
function Chatroom()
{
this.members = {}
}
Chatroom.prototype = {
addMember: function(member)
{
this.members[member.name] = member
member.chatroom = this
},
send: function(message, fromMember, toMember)
{
toMember.receive(message, fromMember)
}
}
const chat = new Chatroom()
const bob = new Member("Bob")
const john = new Member("John")
const tim = new Member("Tim")
chat.addMember(bob)
chat.addMember(john)
chat.addMember(tim)
bob.send("Hey, John", john)
john.send("What's up, Bob", bob)
tim.send("John, are you ok?", john)