-
Notifications
You must be signed in to change notification settings - Fork 2
/
MediatorChatRoom.java
92 lines (74 loc) · 2.22 KB
/
MediatorChatRoom.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Mediator pattern - classic illustration of this pattern
Key idea is that every person has a reference to the chat room (the mediator),
but at no point does one person have a reference to another person.
*/
package behavioral.mediator;
import java.util.ArrayList;
import java.util.List;
// participant in chat room
class Person
{
public String name;
public ChatRoom room; // reference to the room
private List<String> chatLog = new ArrayList<>();
public Person(String name) {
this.name = name;
}
public void receive(String sender, String message)
{
String s = sender + ": '" + message + "'";
System.out.println("[" + name + "'s chat session] " + s);
chatLog.add(s);
}
public void send(String message)
{
room.broadcast(name, message);
}
public void privateMessage(String who, String message)
{
room.message(name, who, message);
}
}
// Mediator
class ChatRoom {
private List<Person> people = new ArrayList<>();
public void join(Person p)
{
String joinMsg = p.name + " joins the room";
broadcast("room", joinMsg);
p.room = this; // adds reference to the person
people.add(p);
}
public void broadcast(String source, String message)
{
for(Person person : people)
if(!person.name.equals(source)) // ensures you don't send message to sender
person.receive(source, message);
}
public void message(String source, String destination,
String message)
{
people.stream()
.filter(p -> p.name.equals(destination))
.findFirst()
.ifPresent(person -> person.receive(source, message));
}
}
class ChatRoomDemo
{
public static void main(String[] args) {
final ChatRoom room = new ChatRoom();
Person john = new Person("John");
Person jane = new Person("Jane");
room.join(john);
room.join(jane);
john.send("hi room");
jane.send("oh, hey john");
Person simon = new Person("Simon");
room.join(simon);
simon.send("hi everyone!");
jane.privateMessage("Simon",
"glad you could join us!");
}
}