-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopic.java
More file actions
54 lines (45 loc) · 1.39 KB
/
Copy pathTopic.java
File metadata and controls
54 lines (45 loc) · 1.39 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
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
public class Topic {
public final String name;
private final List<Agent> subs; // מאזינים
private final List<Agent> pubs; // מפרסמים
// public בנאי
public Topic(String name) {
this.name = name;
this.subs = new ArrayList<>();
this.pubs = new ArrayList<>();
}
public void subscribe(Agent agent) {
if (!subs.contains(agent)) {
subs.add(agent);
}
}
public void unsubscribe(Agent agent) {
subs.remove(agent);
}
public void publish(Message msg) {
// שליחת ההודעה לכל המאזינים
for (Agent agent : subs) {
agent.callback(name, msg);
}
}
public void addPublisher(Agent agent) {
if (!pubs.contains(agent)) {
pubs.add(agent);
}
}
public void removePublisher(Agent agent) {
pubs.remove(agent);
}
// Add this method to return an unmodifiable list of subscribers
public List<Agent> getSubs() {
return Collections.unmodifiableList(subs);
}
// Add this method to return an unmodifiable list of publishers
public List<Agent> getPubs() {
return Collections.unmodifiableList(pubs);
}
}