-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMediator.java
More file actions
54 lines (42 loc) · 1.36 KB
/
Mediator.java
File metadata and controls
54 lines (42 loc) · 1.36 KB
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
import java.util.ArrayList;
import java.util.List;
class Participant {
static int currIndex = 0;
int value = 0;
Mediator mediator;
int index = currIndex++;
public Participant(Mediator mediator) {
this.mediator = mediator.register(this);
}
public void say(int n) {
mediator.publish(index, n);
}
}
class Mediator {
List<Participant> participants = new ArrayList<>();
public void publish(int index, int n) {
for (int i = 0; i < participants.size(); i++) {
if (i != index) {
participants.get(i).value += n;
}
}
System.out.print(String.format("Participant %d broadcasts the value %d. We now have ", index+1, n));
for (int i = 0; i < participants.size(); i++) {
System.out.print(String.format("Participant %d value = %d, ", i + 1, participants.get(i).value));
}
System.out.print(System.lineSeparator());
}
public Mediator register(Participant p) {
participants.add(p);
return this;
}
}
class DemoMediator {
public static void main(String[] args) {
Mediator m = new Mediator();
Participant p1 = new Participant(m);
Participant p2 = new Participant(m);
p1.say(3);
p2.say(2);
}
}